簡體   English   中英

如何在UNIX上找到正在運行的jar文件路徑?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM