macos Mac OS X 上的 Java System.getProperty("user.dir")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/936714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java System.getProperty("user.dir") on Mac OS X
提问by Lawrence Dol
I have an application bundle on Mac OS X 10.4 on the desktop. My application looks for a folder named "resources" in which files to be displayed are kept (kept in the same location as the runnable JAR). I know there is a folder named "Resources" within the app bundle too, sorry if thats confusing, but I never programmed on a Mac and didnt know this would be the same name.
我在桌面上的 Mac OS X 10.4 上有一个应用程序包。我的应用程序查找名为“resources”的文件夹,其中保存要显示的文件(与可运行的 JAR 保存在同一位置)。我知道在应用程序包中也有一个名为“Resources”的文件夹,如果这令人困惑,抱歉,但我从来没有在 Mac 上编程,也不知道这会是同一个名字。
In Windows, when I call System.getProperty("user.dir")
I get the location at which the runnable JAR file is located. Exactly what I wanted.
在 Windows 中,当我调用时,System.getProperty("user.dir")
我会得到可运行 JAR 文件所在的位置。正是我想要的。
Why then when I run an application bundle is the getProperty returning "/"? Thats all. I expected it to return something like "/Users/user_name/Desktop"...which is where my app bundle is located.
为什么当我运行应用程序包时 getProperty 返回“/”?就这样。我希望它返回类似“/Users/user_name/Desktop”的东西......这是我的应用程序包所在的位置。
回答by Lawrence Dol
That's because "user.dir" indicates the current user directory in effect when the JVM is run; in Windows, this is often the location of the JAR unless you specify otherwise. In OSX there may well be no concept of a current dir, but more likely it just has a different default.
那是因为“user.dir”表示JVM运行时生效的当前用户目录;在 Windows 中,这通常是 JAR 的位置,除非您另行指定。在 OSX 中,可能没有当前目录的概念,但更有可能它只是具有不同的默认值。
Though I have never specifically tested this code under OSX, you can try this to locate the directory from which any class was loaded:
虽然我从未在 OSX 下专门测试过这段代码,但您可以尝试使用它来定位加载任何类的目录:
static public File getClassLocation(Class cls, boolean trmjar) {
ClassLoader clsldr; // class loader
URL urlobj; // url object
String exturl; // external form of URL
String lwrurl; // lowercase external form of URL
File rtnfil; // return file
if((clsldr=cls.getClassLoader())==null) { clsldr=ClassLoader.getSystemClassLoader(); }
if((urlobj=clsldr.getResource(cls.getName().replace('.','/')+".class"))==null) {
return null;
}
exturl=urlobj.toExternalForm();
lwrurl=exturl.toLowerCase();
while(lwrurl.startsWith("jar:") || lwrurl.startsWith("file:/")) {
if(lwrurl.startsWith("jar:")) {
if(lwrurl.indexOf("!/")!=-1) { exturl=exturl.substring(4,(exturl.indexOf("!/"))); } // strip encapsulating "jar:" and "!/..." from JAR url
else { exturl=exturl.substring(4 ); } // strip encapsulating "jar:"
}
if(lwrurl.startsWith("file:/")) {
exturl=exturl.substring(6); // strip encapsulating "file:/"
if(!exturl.startsWith("/")) { exturl=("/"+exturl); }
while(exturl.length()>1 && exturl.charAt(1)=='/') { exturl=exturl.substring(1); }
}
lwrurl=exturl.toLowerCase();
}
exturl=java.net.URLDecoder.decode(exturl,"UTF-8");
rtnfil=new File(exturl);
if(lwrurl.endsWith(".class") || (trmjar && lwrurl.endsWith(".jar"))) { rtnfil=rtnfil.getParentFile(); }
if(rtnfil.exists()) { rtnfil=rtnfil.getAbsoluteFile(); }
return rtnfil;
}
it's worked reliably for me for years under Windows for all versions of Java since Java 1.
自 Java 1 以来,它在 Windows 下为所有版本的 Java 可靠地工作了多年。
回答by Lawrence Dol
I instead used the system property "user.home" instead of "user.dir". This way i do not have to worry about where the JVM is looking. I have the application bundle refernce my jar file directly using a bash script as the executeable called by the info.plist file. i can always place the files to be displayed by the app on the users home because that location will always return a path.
我改为使用系统属性“user.home”而不是“user.dir”。这样我就不必担心 JVM 的位置。我有应用程序包直接使用 bash 脚本引用我的 jar 文件作为 info.plist 文件调用的可执行文件。我总是可以将应用程序要显示的文件放在用户家中,因为该位置将始终返回路径。