2017-02-13 197 views
0

我在MyDocuments文件夾中有一個.exe文件。如果我運行C:\User\User\MyDocuments\Sample.exe,我似乎無法在命令提示符下運行該程序。這給了我一個錯誤Failed to execute script無法在CMD中執行腳本

但是,當我在MyDocuments文件夾中打開命令提示符並且只運行Sample.exe時,程序運行完美。

我想要做的是我想要一個Java程序並執行Process p = Runtime.getRuntime().exec("C:\\User\\User\\MyDocuments\\Sample.exe");,它會給我Failed to execute script錯誤。

任何我想到我錯過了什麼?

回答

0

正如Application will not launch from command line full path, but will after CDing to directory建議的那樣,我會確保你的可執行文件不依賴於當前的工作目錄。

嘗試運行:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; 

你嘗試之前並執行可執行文件。

+0

這是Java還是CMD? – Darrius

+0

Powershell。對不起,只是閱讀Java程序部分。嘗試'System.setProperty(「user.dir」,「/ foo/bar」);'而是。在哪裏'user.dir'需要在那裏並且分別改變'/ foo/bar' – Clemsonopoly94

+0

試過了。 'String y = System.getProperty(「user.dir」); 進程p = Runtime.getRuntime()。exec(y +「\\ Sample.exe」);'它工作。謝謝 – Darrius