Question

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

Was it helpful?

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

OTHER TIPS

As fge pointed out in https://stackoverflow.com/a/21903969 , it is important to consume all the output that is produced by the process - not only on Linux, but also on Windows, and not only the standard output, but also the possible errors.

The general pattern for this could look like this:

private static void runCommand(String command) throws IOException
{
    Process process = Runtime.getRuntime().exec(command);
    String errorMessage = 
        new String(toByteArray(process.getErrorStream()));
    String outputMessage = 
        new String(toByteArray(process.getInputStream()));
    int exitValue = 0;
    try
    {
        exitValue = process.waitFor();
    }
    catch (InterruptedException e)
    {
        Thread.currentThread().interrupt();
    }
    System.out.println("Output message: "+outputMessage);
    System.out.println("Error message: "+errorMessage);
    System.out.println("Exit value: "+exitValue);
}

private static byte[] toByteArray(
    InputStream inputStream) throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buffer[] = new byte[8192];
    while (true)
    {
        int read = inputStream.read(buffer);
        if (read == -1)
        {
            break;
        }
        baos.write(buffer, 0, read);
    }
    return baos.toByteArray();
}
"C:\test.exe param1 param2"

You have a tab in there. Try this:

"C:\\test.exe param1 param2"

If the process produces any output on either stdout or stderr you need to consume it. Otherwise it can block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top