aspose file tools*
The moose likes Beginning Java and the fly likes How to return status code from Java to bash shell script? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to return status code from Java to bash shell script?" Watch "How to return status code from Java to bash shell script?" New topic
Author

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

Balaji Krishnan
Greenhorn

Joined: Dec 14, 2011
Posts: 13
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

Joined: Mar 17, 2011
Posts: 8223
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
> 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

Joined: Feb 26, 2001
Posts: 5018
    
    8

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!


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return status code from Java to bash shell script?