.jar ファイルがあります。実行中のどこにいても、そのパスに行きたいです。
私はこのコードを使用します:
System.getProperty("user.dir")
Windowsでは機能しますが、UNIXでは機能しません
これが本当に必要な場合は、次の解決策があります。
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
ただし、きれいとは言えません ;)。他のメカニズムの使用を検討してください。(出典:私のスライドセット)
答えは次のとおりです。
Windows で:
String yourJarName="yourJarName.jar";
String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"").substring(1);
ユニックスで:
String yourJarName="yourJarName.jar";
String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"");