java application文件夹_关于java:如何动态获取Spring Boot Application jar的父文件夹路径?...

我有一个使用java -jar application.jar运行的spring boot Web应用程序。 我需要从代码动态获取jar父文件夹路径。 我该怎么做?

我已经尝试过了,但是没有成功。

欢迎使用Stack Overflow,请在该网站上阅读有关如何提出问题以及如何创建最小,完整和可验证的示例的信息。 始终保持具体,并在询问前尽力而为。 在这种情况下,您提到您尝试过一种解决方案,但是您的错误是什么或基本的实现是什么。

如果我从终端使用java -jar application.jar从该问题出现的代码将返回null。 将代码放在这里:MyCustomClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();如果我使用Spring Tools Suit IDE播放按钮运行应用程序,则此代码的结果将是:D:arquivosrepositoriomyapptrunktargetclasses

好吧,对我有用的是对该答案的改编。

代码是:

if you run using java -jar myapp.jar dirtyPath will be something close

to this: jar:file:/D:/arquivos/repositorio/myapp/trunk/target/myapp-1.0.3-RELEASE.jar!/BOOT-INF/classes!/br/com/cancastilho/service.

Or if you run from Spring Tools Suit, something like this:

file:/D:/arquivos/repositorio/myapp/trunk/target/classes/br/com/cancastilho/service

public String getParentDirectoryFromJar() {

String dirtyPath = getClass().getResource("").toString();

String jarPath = dirtyPath.replaceAll("^.*file:/",""); //removes file:/ and everything before it

jarPath = jarPath.replaceAll("jar!.*","jar"); //removes everything after .jar, if .jar exists in dirtyPath

jarPath = jarPath.replaceAll("%20",""); //necessary if path has spaces within

if (!jarPath.endsWith(".jar")) { // this is needed if you plan to run the app using Spring Tools Suit play button.

jarPath = jarPath.replaceAll("/classes/.*","/classes/");

}

String directoryPath = Paths.get(jarPath).getParent().toString(); //Paths - from java 8

return directoryPath;

}

编辑:

实际上,如果您使用的是Spring Boot,则可以像这样使用ApplicationHome类:

ApplicationHome home = new ApplicationHome(MyMainSpringBootApplication.class);

home.getDir();    // returns the folder where the jar is. This is what I wanted.

home.getSource(); // returns the jar absolute path.

试试这个代码

public static String getParentRealPath(URI uri) throws URISyntaxException {

if (!"jar".equals(uri.getScheme()))

return new File(uri).getParent();

do {

uri = new URI(uri.getSchemeSpecificPart());

} while ("jar".equals(uri.getScheme()));

File file = new File(uri);

do {

while (!file.getName().endsWith(".jar!"))

file = file.getParentFile();

String path = file.toURI().toString();

uri = new URI(path.substring(0, path.length() - 1));

file = new File(uri);

} while (!file.exists());

return file.getParent();

}

URI uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI();

System.out.println(getParentRealPath(uri));

File file = new File(".");

logger.debug(file.getAbsolutePath());

这对我很有用,可以找到我的jar运行的路径,希望这就是您所期望的。

此代码将返回我发出java -jar命令的文件夹路径。 如果我从我的目标文件夹运行它:cd D: arquivos repositorio myapp trunk target java -jar application.jar我会得到以下结果:D:\arquivos

epositorio\myapp\trunk\target\.如果我是从一个外部文件夹运行,请说:cd D : arquivos repositorio myapp trunk java -jar target application.jar不适:D:\arquivos

epositorio\myapp\trunk\.它部分解决了我的问题。 即使我从另一个外部文件夹发出命令,也可以找到路径吗?

这会不会继续创建的新文件。 每次运行脚本?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值