0

I'm trying to read exit value from external program but it's always 0.

String command = ("cmd  /c start /wait "+ Script[0]);                          
Process exec = runtime.exec(command);                   
int waitFor = exec.waitFor();
System.out.println(exec.exitValue);     //always 0   
System.out.println(waitFor); //always 0     

Program is used for programming modules and I need to know if there were any error's.

How to get the application exit value?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    You're getting the exit value from cmd, not your program. – Paul Tomblin Apr 22 '13 at 17:39
  • 1
    Tips: 1) Change `String command = ("cmd /c start /wait "+ Script[0]);` to `String[] command = ("cmd", "/c", "start", "/wait ", Script[0]);` 2) Use a [`ProcessBuilder`](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html) to create the `Process`. 3) Implement all the tips in [When Runtime.exec() won't](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html). – Andrew Thompson Apr 22 '13 at 17:45
  • What is stopping you from running `Script[0]` directly? – Pradeep Pati Apr 22 '13 at 17:56

1 Answers1

1

The program you're actually running is the cmd program, not whatever you're running under that.

See How do I get the application exit code from a Windows command line? for how to extract the underlying exit code.

Community
  • 1
  • 1
Shaun
  • 2,446
  • 19
  • 33