Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

System.exit() capture using bat or shell script

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult for "beginning Java". Moving thread.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic