Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to return an exit code from a batch file?

1,186 views
Skip to first unread message

Sonali Kale

unread,
Oct 27, 2000, 7:22:45 PM10/27/00
to
Hi,

Is there any command which will allow to return an 'exit code'
for a batch file?
I need to return an exit code of success or failure from a batch
file , I am running this batch file through a java program.

Thanks
Sonali

Anthony Borla

unread,
Oct 27, 2000, 10:59:25 PM10/27/00
to
"Sonali Kale" <Sonal...@oracle.com> wrote in message
news:39FA0E45...@oracle.com...

Yes; use the 'choice' utility. Example:

@echo off

REM Return errorlevel 1 (1st letter chosen)
echo A | choice /C:ABCDE /N > nul

REM Return errorlevel 3 (3rd letter chosen)
echo C | choice /C:ABCDE /N > nul

REM Return errorlevel 5 (5th letter chosen)
echo E | choice /C:ABCDE /N > nul

Ensure that this line will be the last line executed by the batch file.

I hope this helps.

Anthony Borla

unread,
Oct 28, 2000, 12:00:48 AM10/28/00
to
"Anthony Borla" <ajb...@bigpond.com> wrote in message
news:mfqK5.10094$e5.2...@newsfeeds.bigpond.com...

> "Sonali Kale" <Sonal...@oracle.com> wrote in message
> news:39FA0E45...@oracle.com...
> > Hi,
> >
> > Is there any command which will allow to return an 'exit code'
> > for a batch file?
> > I need to return an exit code of success or failure from a batch
> > file , I am running this batch file through a java program.
> >
>

I executed the following batch file through the Java routine below. While
the batch very definitely sets the 'errorlevel' value to 5, this is not the
value passed back to the Java program; it, instead, receives a 0 return
code.

Perhaps it is the command interpreter's (in this case Win95 'command.com')
return code which is being provided to the Java program, rather than that of
the batch file.

I hope this helps explain things in case you are not getting the results you
had expected.

// FILE: retcode.bat
@echo off


REM Return errorlevel 5 (5th letter chosen)
echo E | choice /C:ABCDE /N > nul

// FILE: ExecProg.java
public class ExecProg
{
/** Entry Point */
public static void main(String args[])
{
// Check command-line arguments
if (args.length != 1)
{
System.out.println("Usage: progname exefilename");
System.exit(1);
}

// Assemble command-line
StringBuffer cmd = new StringBuffer(args[0]);

// Launch an executable program
try
{
// Create new process
Process process = Runtime.getRuntime().exec(cmd.toString());

// Wait for process to complete
process.waitFor();

// Show some process statistics
System.out.println("Process exit value: " + process.exitValue());
}

catch (IOException e)
{
System.out.println("Error executing program");
System.exit(1);
}

catch (InterruptedException e)
{
System.out.println("Program terminated unexpectedly");
System.exit(1);
}

return;
}
}

// Command Line
java ExecProg retcode.bat

Todd Vargo

unread,
Oct 28, 2000, 2:19:55 AM10/28/00
to
Anthony Borla <ajb...@bigpond.com> wrote in message
news:T8rK5.10115$e5.2...@newsfeeds.bigpond.com...

> "Anthony Borla" <ajb...@bigpond.com> wrote in message
> news:mfqK5.10094$e5.2...@newsfeeds.bigpond.com...
> > "Sonali Kale" <Sonal...@oracle.com> wrote in message
> > news:39FA0E45...@oracle.com...
> > > Hi,
> > >
> > > Is there any command which will allow to return an 'exit code'
> > > for a batch file?
> > > I need to return an exit code of success or failure from a
batch
> > > file , I am running this batch file through a java program.
> > >
> >
>
> I executed the following batch file through the Java routine below. While
> the batch very definitely sets the 'errorlevel' value to 5, this is not
the
> value passed back to the Java program; it, instead, receives a 0 return
> code.
>
> Perhaps it is the command interpreter's (in this case Win95 'command.com')
> return code which is being provided to the Java program, rather than that
of
> the batch file.
>
> I hope this helps explain things in case you are not getting the results
you
> had expected.

[java code snipped]

I think this problem is much like trying to return an exit code from a VB
Shell() statement which is basically invoking the windows ShellExecute()
API, which only returns success/fail status (program found=0/missing>=32).

AFAIK, the only way around this problem is to create a flag file (typically
done in %TEMP%), then you simply check it when the process ends.

I don't know java code but, if it actually does call the ShellExecute API,
the java code will likely continue while the shelled program/batch is still
running. The VB way around this is you would use CreateProcess and
WaitForProcess API's too. Hey Anthony, could you check this prospect in
java? Something like a batch that uses CHOICE to wait for 60 seconds then
create a flag file. Then back in the java code, have the next line display a
MsgBox (to see if the MsgBox is displayed before the file is created).

BTW, I think this question would be better asked in a java group.

--
Todd Vargo (body of msg must contain my name to reply)

Ted Davis

unread,
Oct 28, 2000, 11:51:06 AM10/28/00
to

Assuming this is something along the lines of spawning a secondary
command processor to run the batch program, you may have a problem:
you have to persuade the command processor to return the exit code -
this works in NT (the CHOICE approach is usable if you have Choice.exe
for NT), but not in Win95. There is a clue in the header of your
message that this may be for NT, but it is best to say what OS it is
wanted for.


T.E.D. (tda...@gearbox.maem.umr.edu - e-mail must contain "batch" in the subject or my .sig in the body)

Anthony Borla

unread,
Oct 28, 2000, 6:22:40 PM10/28/00
to
"Todd Vargo" <t...@birdlover.com> wrote in message
news:8tdruh$mn0jt$1...@ID-25025.news.dfncis.de...

> Anthony Borla <ajb...@bigpond.com> wrote in message
> news:T8rK5.10115$e5.2...@newsfeeds.bigpond.com...
> > "Anthony Borla" <ajb...@bigpond.com> wrote in message
> > news:mfqK5.10094$e5.2...@newsfeeds.bigpond.com...
> > > "Sonali Kale" <Sonal...@oracle.com> wrote in message
> > > news:39FA0E45...@oracle.com...
> > > > Hi,
> > > >
<SNIP>

>
> I think this problem is much like trying to return an exit code from a VB
> Shell() statement which is basically invoking the windows ShellExecute()
> API, which only returns success/fail status (program found=0/missing>=32).
>
> AFAIK, the only way around this problem is to create a flag file
(typically
> done in %TEMP%), then you simply check it when the process ends.
>
> I don't know java code but, if it actually does call the ShellExecute API,
> the java code will likely continue while the shelled program/batch is
still
> running. The VB way around this is you would use CreateProcess and
> WaitForProcess API's too. Hey Anthony, could you check this prospect in
> java? Something like a batch that uses CHOICE to wait for 60 seconds then
> create a flag file. Then back in the java code, have the next line display
a
> MsgBox (to see if the MsgBox is displayed before the file is created).
>

Todd,

I found that the Java program behaves as you expected. Unless you include
the following line:

// Wait for process to complete
process.waitFor();

the Java program (parent process) simply continues on leaving the command
interpreter / batch file instance (child process) to complete.

I would expect that the line above is calling the 'WaitForProcess' API or
its equivalent. However, I cannot confirm this as I have not checked Java
library documentation.

I hope this helps.

Anthony Borla

Todd Vargo

unread,
Oct 29, 2000, 1:38:49 AM10/29/00
to
Anthony Borla <ajb...@bigpond.com> wrote in message
news:1aIK5.10511$e5.3...@newsfeeds.bigpond.com...

Cool, Java has this function built in. This extra info should be useful to
Sonali.

0 new messages