Search code examples
javawindows-server-2008-r2runtime.execjava-6

Java runtime.exec does not execute correctly


I am getting an exe-File, which I have to execute using Java (Version 6) on Windows Server 2008 R2. Now there is s problem I do not really understand. When executing the file with the commandline

"C:\test.exe param1 param2" 

it works correctly, but when I execute the file with

Process proc = Runtime.getRuntime().exec("C:\\test.exe param1 param2");
proc.waitFor();

I can see the test.exe in the windows task manager and it starts running (it creates a log which states that), but then it simply doesn't do anything anymore. The test.exe endlessly runs with 0% and I have to kill the process manually. After doing so the java-program continues and

proc.exitValue()

is "1", therefore java recognizes that I have killed the process. I also tried writing the commandline in a batchfile and executing it with .exec() but it didn't change anything.

What really confuses me, is that it runs perfectly via windows command-line, but does not via .exec(). Does anyone have an idea what might cause such a problem? Or is it more likely that the test.exe is causing the problem?

In best regards

Edit: Wrote down the wrong path in .exec


Solution

  • Since your program procudes a lot of output, my hypothesis is that it is stuck trying to write to the standard output (which is a pipe under Linux, don't know for Windows).

    Try this:

    final byte[] devnull = new byte[1024];
    
    final ProcessBuilder builder = new ProcessBuilder("C:\\test.exe", "param1", "param2")
        .redirectErrorStream(true);
    final Process p = builder.start();
    
    final InputStream stdout = process.getInputStream();
    
    // Purge stdout
    while (stdout.read[devnull] != -1);
    
    // Grab the process' exit code here