4

I am executing Java class from inside my application.

proc = Runtime.getRuntime().exec("java Test");

How can I recognize whether Test executed successfully or not (i.e. no exceptions)?


Redirecting output / error:

proc = Runtime.getRuntime().exec(new String[] {
    "java",
    mclass,
    ">NUL 2>test.txt"
});

From cmd:

java Main >NUL 2>test.txt
Little Jeans
  • 286
  • 2
  • 4
  • 11

6 Answers6

7
process.waitFor();
int exitCode = process.exitValue();
if(exitCode == 0) { // success }
else { // failed }

This works, if the Test is designed properly and returns appropriate exit codes (generally, >0 if something went wrong).

If you want to get Tests output/error message to determine what was wrong, you should get proc.getInputStream() (this returns the output stream of the child process), proc.getErrorStream() and read from the input streams in separated threads.

Note that the child process will get blocked if it writes to error/output stream and there are no readers. So reading error/output streams of the process is useful in any cases.

Another option to avoid child blocking is to redirect its error/output to a file and/or to /dev/null ('NUL' for windows):

Runtime.exec("java Test >/dev/null 2>&1");
Runtime.exec("java Test >/dev/null 2>erroroutput");
hmartos
  • 861
  • 2
  • 10
  • 19
khachik
  • 28,112
  • 9
  • 59
  • 94
  • Thank you! I've tried redirecting. But when I run it from windows `cmd`, it works fine. Whereas when I use `exec` or process builder, my application just stops and nothing happens, so I have to terminate it. Any advice? – Little Jeans Dec 05 '10 at 10:26
  • It is difficult to say definitely. Try to redirect to a file. Does it contain anything after execution? – khachik Dec 05 '10 at 10:30
  • Please, take a look at my update. Am I doing it right? File contains nothing either using `cmd` or `exec`. – Little Jeans Dec 05 '10 at 10:43
  • Well, although `NIL` still does not work for me, I've decided to put error & output streams to separate threads. Thanks for your help. – Little Jeans Dec 05 '10 at 11:29
  • just to not confuse future readers. There is no `exitCode()` method in `Process` class. Only `exitValue()`. – Little Jeans Dec 05 '10 at 11:59
2

Redirection is done by the shell processor, not by Runtime.exec() (at least not on Windows).
You need to execute your command by cmd.exe:

String command = "cmd /c java -classpath D:\\dev\\temp\\ Main >NUL 2>test.txt";
proc = Runtime.getRuntime().exec(command);
user85421
  • 28,957
  • 10
  • 64
  • 87
  • Thanks, it works. What if I have to have OS-independent solution? – Little Jeans Dec 05 '10 at 19:38
  • you can check the "os.name" system property (System.getProperty) and call the appropriate shell command (sh or cmd). Or a little hack: in Unix/Linux create a link called "cmd" that points to the shell command... – user85421 Dec 05 '10 at 20:59
1

See the Process class

You can call proc.waitFor() to return an integer value. But you have to make sure that all output of the program is handled correctly (e.g. use the proc.getInputStream() method).

Marc
  • 3,550
  • 22
  • 28
1

Have you tried proc.exitValue() ?

Filip Spiridonov
  • 34,332
  • 4
  • 27
  • 30
0

You may use ProcessBuilder class of java to execute these files.

Rajat Bhadauria
  • 260
  • 2
  • 12
0

Errr... what? Do it this way:

int response = (int)(getClass().getClassLoader().findClass("Test").
        getMethod("someStaticMethod", new String[]{}).
        invoke(null, new Object[]{}));

This invokes the static method "int someStaticMethod()" of the class "Test" which gets loaded dynamically.

thejh
  • 44,854
  • 16
  • 96
  • 107