Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes return code from a java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "return code from a java program " Watch "return code from a java program " New topic

return code from a java program

Maalti Iyer
Ranch Hand

Joined: Jul 11, 2001
Posts: 52

I've a Unix shell script that calls multiple (4) java programs. It should call a java program in the sequence only if the provious program was successful.(ie program 2 should be called only if program 1 was successful).

can someone help me?

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Use System.exit() in your Java programs to return an error level to the UNIX shell.

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
To add to what Tom said, in Unix it is traditional to exit with a zero to indicate success. Any other value indicates some sort of error occured. In some programs the exit value tells you which error occured.

<a href="" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3640
Originally posted by Tom Blough:
Use System.exit() in your Java programs to return an error level to the UNIX shell.

If you do System.exit(-1);, it will kill your App Server
[ October 28, 2005: Message edited by: Chetan Parekh ]

My blood is tested +ve for Java.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

After implementing the 'System.exit (status);' -stuff, you combine the commands like this:

to execute P2 only if P1 succeeded, and so on.
I agree. Here's the link:
subject: return code from a java program
It's not a secret anymore!