簡體   English   中英

從不同目錄啟動應用程序(.jar文件)時,如何獲取位於類路徑中的絕對文件路徑?

[英]How to get absolute file path located in class path when starting application (.jar file) from different directory?

假定Java應用程序文件夾包含以下文件:

app.jar
lib/file.exe

在app.jar中,我這樣調用.exe:

String[] exeCmd = new String[]{"lib/file.exe"};
ProcessBuilder pb = new ProcessBuilder(exeCmd);
...

如果我從同一目錄啟動.jar文件-一切正常。 但是,如果從不同目錄啟動了應用程序- lib/file.exeProcessBuilder退出,錯誤為“無此文件或目錄”。

我的問題是-盡管從其他目錄啟動了應用程序,如何獲取正確的lib/file.exe路徑並將其提供給ProcessBuilder

編輯:

現在,我在想(不同的方式)那些.exe之類的文件,我應該放在用戶目錄中,例如Windows OS中的“應用程序數據”,以后再使用它們嗎? 通過調用System.getenv("APPDATA");可以很容易地找到路徑System.getenv("APPDATA"); System.getProperty("user.home"); 在Unix中。 但另一方面,如果有很多PC用戶,則將為每個用戶復制這些.exe文件(浪費磁盤空間)。

您可以這樣做:

YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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