1

I am trying to write a program that calls external jars from the command line. In my code it will do java -jar test,jar args. What I want to know though is if a error occurs in this external jar, how to catch it in my java program so I can do the necessary procedure? This is a new zone of coding for me from college level so I am a little clueless.

RandomUser
  • 15
  • 1
  • 7
  • This is just a guess, but I believe it should be possible to capture the output of the java command as well as its exit code. If the exit code is non-zero, you would then know that the output from the java command has error information. – thatidiotguy Nov 24 '15 at 16:25
  • The external Jar can throw exceptions which you catch in your application. But that relies on the external Jar actually throwing exceptions. – Emz Nov 24 '15 at 16:27
  • Could you give more details on how you run these external commands @user3600861 ? – smonff Nov 24 '15 at 16:46

1 Answers1

0

Command-line programs returns exit status when finished executing it's work (e.g. zero when everything is ok).

You should be able to retrieve something interesting by storing the return value of your system call and test it according to what you want to do.

// Code from https://stackoverflow.com/questions/8496494/
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar test.jar args");

 // Check retVal to test
int retVal = pr.waitFor();

More about this in this SO question.

smonff
  • 3,399
  • 3
  • 36
  • 46