开发者

Problem with Runtime Process Execution

开发者 https://www.devze.com 2023-01-06 07:19 出处:网络
Can somebody find what is wrong开发者_JAVA百科 with this code: Runtime rt = Runtime.getRuntime();

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

精彩评论

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

关注公众号