File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.exit() capture using bat or shell script

 
Kumar Jaya
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kumar Jaya
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning Java". Moving thread.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic