Java 如何动态获取spring boot应用程序jar父文件夹路径?
我有一个SpringBootWeb应用程序,我使用java-jar application.jar运行它。我需要从代码中动态获取jar父文件夹路径。我怎样才能做到这一点 我已经试过了,但没有成功Java 如何动态获取spring boot应用程序jar父文件夹路径?,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个SpringBootWeb应用程序,我使用java-jar application.jar运行它。我需要从代码中动态获取jar父文件夹路径。我怎样才能做到这一点 我已经试过了,但没有成功 File file = new File("."); logger.debug(file.getAbsolutePath()); 这为我找到了我的jar运行的路径,我希望这是您所期望的。好吧,对我来说有效的是对的改编。 代码是: 如果您使用java-jarmyapp.jar运行,那么d
File file = new File(".");
logger.debug(file.getAbsolutePath());
这为我找到了我的jar运行的路径,我希望这是您所期望的。好吧,对我来说有效的是对的改编。 代码是: 如果您使用java-jarmyapp.jar运行,那么dirtyPath将非常接近 到这个:jar:file:/D:/arquivos/repositorio/myapp/trunk/target/myapp-1.0.3-RELEASE.jar/BOOT-INF/classes/br/com/cancastilho/service。 或者,如果您使用Spring Tools套装运行,则如下所示: 文件:/D:/arquivos/repositorio/myapp/trunk/target/classes/br/com/cancastilho/service 编辑: 实际上,如果您使用的是spring boot,您可以这样使用类:
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));
欢迎来到Stack Overflow,请在此网站阅读和。在询问之前,一定要明确,并尽最大努力。在本例中,您提到您尝试了一个解决方案,但您的错误是什么,或者您的基本实现是什么。我将把代码放在这里:
MyCustomClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()代码>如果我使用Spring Tools Suit IDE play按钮运行应用程序,此代码的结果将是:/D:/arquivos/repositorio/myapp/trunk/target/classes/
此代码将返回我发出java-jar命令的文件夹路径。如果我从我的目标文件夹cd D:\arquivos\repositorio\myapp\trunk\target java-jar application.jar运行它,我将得到以下结果:D:\arquivos\repositorio\myapp\trunk\target\。
如果我从外部文件夹运行它,比如说:cd D:\arquivos\repositorio\myapp\trunk\java-jar target\application.jar我会得到:D:\arquivos\repositorio\myapp\trunk\。
它部分解决了我的问题。即使我从另一个外部文件夹发出命令,也有办法找到路径吗?这不会继续创建的新文件。每次脚本运行时?
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));