• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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: 23956
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: 79704
381
  • 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.
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic