WSK
WSK

Reputation: 6198

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)

Upvotes: 1

Views: 2882

Answers (2)

GHad
GHad

Reputation: 9641

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

Upvotes: 4

jjnguy
jjnguy

Reputation: 138972

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 )

Upvotes: 1

Related Questions