Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return status code from Java to bash shell script?

 
Balaji Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am invoking a java program from bash shell script. I want to get the return status of the java program in shell. If there is a run time exception or processing error occurs, set return code to 1 and get in the shell script. I know System.exit(1) would do that, but it also stops JVM instance(which is costly and i don't want that to happen).

Is there any other possible way to perform this?

Thanks!
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balaji Krishnan wrote:I am invoking a java program from bash shell script. I want to get the return status of the java program in shell. If there is a run time exception or processing error occurs, set return code to 1 and get in the shell script. I know System.exit(1) would do that, but it also stops JVM instance(which is costly and i don't want that to happen).

Is there any other possible way to perform this?

Not that I know of, since control won't return to the shell until the JVM exits. I suppose you could execute the program in the background and have it communicate via some sort of flag file, but that is likely to be a lot slower than simply exiting the program.

Also: if your program throws an exception, why wouldn't you want it to exit?

Don't start worrying about efficiency until you have a working solution. And even then, don't start putting in kluges until you can prove that your solution is too slow.

Donald Knuth wrote:Premature optimization is the root of all evil.
and premature speculative optimization is the worst kind of all.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I want to get the return status of the java program in shell.

Well the return status of a program (java or not) is returned in the moment the program exits.
If an exception occurs, then either you can recover from there, remedy the situation and go on performing the wished task,
or you can not recover and the mission has failed. It is appropriate to return a return code meaning "failure" in the second case, and then System.exit is good enough.
In the first case you should not produce a return code yet and you should let the program proceed.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Don't start worrying about efficiency until you have a working solution. And even then, don't start putting in kluges until you can prove that your solution is too slow.

Donald Knuth wrote:Premature optimization is the root of all evil.
and premature speculative optimization is the worst kind of all.

Amen to that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic