Java.io.IOException: Cannot start program "dir": CreateProcess error = 2, Das System

Hello, I am trying to run the following code in eclipse:

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

In clear terms, 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 the following error message:

java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das System kann die angegebene Datei nicht finden
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
....

When I try to use the code in the cmd field, it works fine. My code is:

    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) {
            System.out.println(line);
        }

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

    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}
+4
source share
1 answer

Add "cmd.exe /c"to the top of your command line, which should do the trick.

Change . The parameter /cwill force the cmdreturn to the java process. Without it, the process freezes.

+7
source

All Articles