开发者

Problem with Runtime Process Execution

Can somebody find what is wrong开发者_JAVA百科 with this code:

Runtime rt = Runtime.getRuntime();
Process pr;
File myFolder = new File("C:\\Temp");
pr = rt.exec("myExec.bat", null, myFolder);
pr.waitFor();
pr.destroy();

When I run this code, I get following exception (while file and folder used exist as specified):

java.io.IOException: Cannot run program "myExec.bat" (in directory "C:\Temp"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at com.radml.radmlp.main(Test.java:10)


rt.exec expects a file with no path information to be in the user dir and not in the directory you specify to use as working directory. Using it this way

    Runtime rt = Runtime.getRuntime();
    Process pr;
    File myFolder = new File("C:\\Temp");
    pr = rt.exec(new File(myFolder, "myExec.bat").getAbsolutePath(), null, myFolder);
    pr.waitFor();
    pr.destroy();

should work as long as your file c:\Temp\myExec.bat exists.

Greetz, GHad


Have you made sure that your bat file is located in "C:\Temp\myExec.bat"?

(Just a guess, but make sure the file isn't actually called C:\Temp\myExec.bat.txt )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜