aspose file tools*
The moose likes Java in General and the fly likes System.exit() capture using bat or shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "System.exit() capture using bat or shell script" Watch "System.exit() capture using bat or shell script" New topic
Author

System.exit() capture using bat or shell script

Kumar Jaya
Ranch Hand

Joined: Jan 12, 2009
Posts: 45
Hello,

I am having a simple jar file helloworld.jar with a main class helloworld.class which has one statement System.exit(1). I am calling this jar file using java -jar helloworld.jar command. I am using a bat file in windows to execute the above command. Is there a way to capture the value 1 that is been returned by System.exit(1) method. I want to do that in linux shell script also. Thanks in advance.

Regards
Jaya

Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
Hi Jaya,

System.exit(value) does not return the value to OS (batch file or shellscript) but it returns to JVM which JVM uses to see if the program was terminated correctly or not. Read Java specifications for the same. Why do you want this value??

Thanks and Regards,
Vikash Anand.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2416
    
    7

Umm the "integer" in System.exit(int) indicates whether the exit completes successfully (a zero) or with some error (not a zero eg 1). So I doubt you can print the non-zero value.

However if you have exceptions caught you can use the System.err.println() to print out the errors.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Vikash Ananda wrote:
System.exit(value) does not return the value to OS (batch file or shellscript) but it returns to JVM which JVM uses to see if the program was terminated correctly or not. Read Java specifications for the same. Why do you want this value??


Actually, this "value" does eventually get back to the OS (batch file and shellscript). With the batch file, it gets back as the error level, which can be used by the batch to act accordingly. With shellscripts, this value becomes the exit status of the application. And how the shellscripts accesses it depends on the shell being used.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kumar Jaya
Ranch Hand

Joined: Jan 12, 2009
Posts: 45
Hi Vikash,

I want that value in my bat/script file to make decisions to display to the user. Is there any way in my bat/sript file where I can trap the values returned by a java program??

Regards
Jaya
Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
Hi,

I do not know how to capture that in a batch script but using another program you can do the same: Try code below:



The line int exitVal = proc.waitFor(); will give the process exit value.

Try it and let us know. Is this what you were looking at??

Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
Henry Wong wrote:
Vikash Ananda wrote:
System.exit(value) does not return the value to OS (batch file or shellscript) but it returns to JVM which JVM uses to see if the program was terminated correctly or not. Read Java specifications for the same. Why do you want this value??


Actually, this "value" does eventually get back to the OS (batch file and shellscript). With the batch file, it gets back as the error level, which can be used by the batch to act accordingly. With shellscripts, this value becomes the exit status of the application. And how the shellscripts accesses it depends on the shell being used.

Henry


Hi Henry,

Thanks for the info.
Kumar Jaya
Ranch Hand

Joined: Jan 12, 2009
Posts: 45
Hi Vikash,

Yes, but I cant afford to write a java code, but I got it through Henry's words. I got the exit value using the bat/script commands. Thanks for the information Vikash.

Henry,

Thanks for the information..

Regards
Kumaravel J
Amol Talap
Greenhorn

Joined: Mar 23, 2004
Posts: 2
Kumaravel J : May I know the fix you made. I am struggling for the same as my tracking process in shell script looks for the return/exit status.
Kumar Jaya
Ranch Hand

Joined: Jan 12, 2009
Posts: 45
Hi Amol,

Try this in your script file, I assume that the jar file you want to run is sample.jar

java -jar sample.jar
RETURN=$?


Now the RETURN has the value of the executed command

Regards
Kumaravel J
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Too difficult for "beginning Java". Moving thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.exit() capture using bat or shell script