【问题标题】:How to get spring boot application jar parent folder path dynamically?如何动态获取spring boot应用程序jar父文件夹路径?
【发布时间】:2018-03-21 07:07:11
【问题描述】:

我有一个使用 java -jar application.jar 运行的 Spring Boot Web 应用程序。我需要从代码中动态获取 jar 父文件夹路径。我怎样才能做到这一点?

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

【问题讨论】:

  • 欢迎来到 Stack Overflow,请阅读本网站中的 How to Ask a QuestionHow to create a Minimal, Complete, and Verifiable example。在询问之前,请始终保持具体并尽最大努力。在这种情况下,您提到您尝试了一个解决方案,但您的错误是什么或您的基本实现是什么。
  • 如果我从终端使用 java -jar application.jar,则该问题上显示的代码将返回 null。我将代码放在这里:MyCustomClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); 如果我使用 Spring Tools Suit IDE 播放按钮运行应用程序,此代码的结果将是:/D:/arquivos/repositorio/myapp/trunk/target/classes/

标签: java spring spring-boot


【解决方案1】:
    File file = new File(".");
    logger.debug(file.getAbsolutePath());

这对我来说是为了获得我的 jar 运行的路径,我希望这是你所期望的。

【讨论】:

  • 此代码将返回我发出 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\. 它部分解决了我的问题。即使我从另一个外部文件夹发出命令,有没有办法找到路径?
  • 这不会继续创建 .每次运行脚本?
【解决方案2】:

嗯,对我有用的是对this answer 的改编。 代码是:

如果你使用 java -jar myapp.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 Suite 运行,则如下所示: 文件:/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.

【讨论】:

    【解决方案3】:

    试试这个代码

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 2011-05-08
      • 2011-07-31
      • 2015-06-21
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多