user2889693 user2889693 -4 years ago 446
Java Question Cannot run program "dir": CreateProcess error=2, Das System

Hello I try to run the following cmd code in eclipse:

"DIR \""+DEV_HOME+"\\src\"\\*.java /b /s >> \""+DEV_HOME+"\\bin\\javaFiles.txt\""

In clear it looks like this:

DIR "D:\Thomas\Dokumente\Daten\workspace\WBRLight\src"\*.java /b /s >> "D:\Thomas\Dokumente\Daten\workspace\WBRLight\bin\javaFiles.txt"

But I get following error message: Cannot run program "dir": CreateProcess error=2, Das System kann die angegebene Datei nicht finden
at java.lang.ProcessBuilder.start(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(

When I try to use the code in the cmd box, Its working fine.
My code:

public void run_cmdLine(String command) {
try {
Runtime rt = Runtime.getRuntime();
BufferedReader input = null;
Process pr = null;

pr = rt.exec(command);
input = new BufferedReader(new inputStreamReader(pr.getInputStream()));

String line = null;

while ((line = input.readLine()) != null) {

int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);

} catch (Exception e) {

Answer Source

Add "cmd.exe /c" at the beginning of your command string, that should do the trick.

Edit: The /c parameter will make the cmd finish an return it to the java process. Without it, the process will hang.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download