我有一个.jar文件。我希望它在运行时的路径。
我使用此代码:
System.getProperty("user.dir")
它适用于Windows,但无法在unix中使用
答案 0 :(得分:0)
如果你真的需要这个,这是一个解决方案:
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
然而,它远非漂亮;)。考虑使用其他机制。 (来源:a slideset of mine)
答案 1 :(得分:0)
答案是:
在Windows中:
String yourJarName="yourJarName.jar";
String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"").substring(1);
在unix中:
String yourJarName="yourJarName.jar";
String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"");