【问题标题】:Get relative path from jar file从jar文件中获取相对路径
【发布时间】:2018-11-11 05:50:08
【问题描述】:

我试图在 Ubuntu 中找到一个相对于当前 jar 文件的特殊路径。 在 Windows 中它可以正常工作:

String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");

但是,在 Linux 中,我总是得到 jar 文件,但我无法将两个目录退回到 configurationFile.xml

/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory

但是,如果我这样做了

pwd /some/directory/where/xml/is/located/xyz.jar/../../

它没有任何问题。

我在这里做错了什么?

我想不通。

【问题讨论】:

  • 尝试使用 File.separator 代替斜杠:new File(File.separator + "home" + File.separator + "somewhere");
  • 非常感谢!我会试试的。

标签: java path relative-path


【解决方案1】:

仅使用路径中的目录。 确定 jar 文件的路径后,提取其目录的路径并仅使用目录。

【讨论】:

  • 嗨,这很好用。我拆分了路径并按照我需要的方式重新连接它,现在它可以工作了!非常感谢。
猜你喜欢
  • 1970-01-01
  • 2014-07-22
  • 2019-05-04
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
相关资源
最近更新 更多