2017-02-13 165 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