【发布时间】: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