簡體   English   中英

相對於jar的Java執行文件

[英]Java execute file relative to jar

我有一個ac#控制台應用程序,我將使用此處介紹的技術向您發送使用Java的建議, 向控制台應用程序發送命令?

我的問題是,控制台應用程序位於與jar相同的文件夾中,並且我找不到如何執行與jar相同目錄的文件。

我在Java中嘗試了很多東西,例如

Process pr = Runtime.getRuntime().exec("runtime_libraries/consoleapp.exe");

但是它說找不到文件。

java.io.IOException: CreateProcess error=2, The system cannot find the file specified

我認為這很簡單,但顯然並非如此。

所以我的問題是,如何將相對路徑放入Runtime exec?

每當您遇到此類問題時,都需要進行一些初步檢查...

  1. 使用System.out.println(System.getProperty("user.dir"));驗證您當前的運行位置System.out.println(System.getProperty("user.dir")); 並確保它在您期望的位置;
  2. 使用System.out.println(new File("runtime_libraries/consoleapp.exe").exists()));驗證consoleapp.exe是否存在System.out.println(new File("runtime_libraries/consoleapp.exe").exists()));

大多數IDE允許您在開發過程中更改工作目錄。 在生產中,應確保正確設置了工作目錄(例如,在Windows下,可以將快捷方式配置為在指定位置“啟動”)

暫無
暫無

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

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