简体   繁体   中英

Problem with Runtime Process Execution

Can somebody find what is wrong 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM