Solved

Pass Exit Status to Windows Batch File

Posted on 2002-07-03
17
938 Views
Last Modified: 2008-03-04
In Windows NT, I have a batch file that contains something like this:

java MyProgram
cd\SomeDirectory
SomeProgram.exe
...

The MyProgram.java program contains System.exit() calls with different exit status codes. How do I write the batch file so that it reads the exit status code from MyProgram? Depending on the status code, the batch file will either stop or continue with the next command.

Thanks in advance.
0
Comment
Question by:yongsing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 7126578
Try in the batch file:

if errorlevel 0 <some command>
if errorlevel 1 <do something else>

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7126783
below is the .bat file that I created several days ago for a similar purpose. It's not tested on all Winxx versions, so all feedback is welcome

***********

@echo off

echo.
echo.

set ERRORLEVEL=
  set c=%CLASSPATH%
  set CLASSPATH=%CLASSPATH%;shellApp.zip

java shell

  set CLASSPATH=%C% >&nul
  set C= >&nul

echo.
echo ERRORLEVEL: %ERRORLEVEL%
echo.

IF NOT %ERRORLEVEL% == 101 GOTO Shell_err
    rem normal exit (Shell exit code 101)
goto exit


:Shell_err
IF NOT %ERRORLEVEL% == 102 GOTO Shell_notstarted
    rem error exit (Shell exit code 102)
echo.
echo.
echo Shell tool exits with error !
echo.
echo Please send an e-mail to admin@company.com
echo.
echo.
pause
goto exit
   
:Shell_notstarted
echo.
echo.
echo Could not start Shell tool !
echo.
echo Please send an e-mail to admin@company.com
echo.
echo.
pause
goto exit



:exit
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7127111
My guess was correct: the following demonstrates it (put the batch file in the same directory as the java class):

public class ExitCode {
     public static void main(String[] args){
          int exitCode = 0;
          if (args.length < 1){
               exitCode = -1;
          }
          else {
               exitCode = Integer.parseInt(args[0]);
          }
          System.out.println("Java application exiting with code of " + exitCode);
         System.exit(exitCode);
     }
}

/*

Batch file:

@echo off
echo Testing exit code of java programme with parameter of 9...
java -cp . ExitCode 9
if errorlevel 9 echo It worked!
echo Testing exit code of java programme with no parameters...
java -cp . ExitCode
if errorlevel -1 echo It worked again!


*/
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 16

Expert Comment

by:heyhey_
ID: 7127126
my .bat file is "better" because it detects if java VM is missing or misconfigured :)
0
 
LVL 9

Author Comment

by:yongsing
ID: 7128965
Thanks to the both of you. Since CEHJ was the first to response with a workable solution, I shall award him/her the points.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129175
I do not agree but what the hell ...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7129465
Come on heyhey! He's got working, simple code. And by the way, is it really a good idea to print, come what may, the alarming word 'ERRORLEVEL' to stdout? :-)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129660
>  is it really a good idea to print,

pretty good idea - if some idiot has problems with your app, all you have to do is ask him for a screenshot :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7129666
But it prints the word 'ERRORLEVEL' even if there hasn't been an error!
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129675
call it "exit code" if you want. I do not see a problem.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129683
on a side note - normal "idiots" will start the app by double clicking on the .bat file (or shortcut to it). if app executes successfully, command prompt window is (usually) automatically closed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7129686
So they won't see the error message even if there *is* one :-)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129690
hint

THERE IS A DOS COMMAND NAMED "pause"

can you guess what exactly it is supposed to do ?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129695
I will stop responding on this question because I may become more rude than want to.

Have a nice day.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7129697
Ah yes, I missed that line :-)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7129700
<censored>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7129704
>>I will stop responding on this question

Come on heyhey - we've got work to do :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is there a simpler dropbox system? 10 44
glassfish admin console 1 23
IBM TS2900 (3572) Tape Autoloader Java? 12 61
Tomcat 9 + java 8 error while trying to deploy a war file 2 731
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question