![](/img/trans.png)
[英]When running java how do I get around the need to enter the full path of my JAR file?
[英]How I find my running jar file path on unix?
我有一個.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);
在Unix中:
String yourJarName="yourJarName.jar";
String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.