Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get error message when excuting java command?

Tags:

java

I call a class which is located somewhere in a jar file (using java -classpath path/file.jar classname) within my java code.

This work well but only if the command is well formed. If I make a mistake the getRuntime().exect(command) just doesn't say anything. Bellow I have the working command invocation. I would like to get the error message when the command doesn't work. If I make a mistake in a cmd (windows) I get a proper error and I can fix it. But not within my java application.

I left a 'if(input.ready())' since if I don't the program freezes when the command line is incorrect. This happens when executing 'input.readLine()'.

        // Execute a command with an argument that contains a space
        String[] genKOSCommand = new String[] {
                "java",
                "-classpath",
                Config.XDSI_TEST_KIT_HOME + "/xdsitest/lib/xdsitest.jar;"
                        + Config.XDSI_TEST_KIT_HOME + "/xdsitest/classes",
                "ca.etsmtl.ihe.xdsitest.docsource.SimplePublisher", "-k",
                "C:/Softmedical/Viewer_Test/xdsi-testkit-2.0.4/xdsihome/usr/data/image14.dcm" };

        Process child = Runtime.getRuntime().exec(genKOSCommand);

        BufferedReader input = new BufferedReader(new InputStreamReader(
                child.getInputStream()), 13107200);

        String line = null;

        if (input.ready()) {
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }

            try {
                child.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Have any advice on how to get an error from the executed command?

Thank you

like image 353
code-gijoe Avatar asked Apr 21 '11 18:04

code-gijoe


2 Answers

By using getErrorStream:

BufferedReader errinput = new BufferedReader(new InputStreamReader(
                child.getErrorStream()));

When processing the input from the different streams, it is better to do it in a different thread (since those calls (readLine etc.) are blocking calls.

like image 190
MByD Avatar answered Sep 19 '22 20:09

MByD


Here's a bit more complete piece of code to print out errors received upon running some command via Process/Runtime:

final String command = "/bin/bash -c cat foo.txt | some.app";
Process p;
    try {
        p = Runtime.getRuntime().exec(command);
    } catch (final IOException e) {
        e.printStackTrace();
    }

    //Wait to get exit value
    try {
        p.waitFor();
        final int exitValue = p.waitFor();
        if (exitValue == 0)
            System.out.println("Successfully executed the command: " + command);
        else {
            System.out.println("Failed to execute the following command: " + command + " due to the following error(s):");
            try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
                String line;
                if ((line = b.readLine()) != null)
                    System.out.println(line);
            } catch (final IOException e) {
                e.printStackTrace();
            }                
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
like image 43
laylaylom Avatar answered Sep 18 '22 20:09

laylaylom