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
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.
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
[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)
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)
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
Cool, Java has this function built in. This extra info should be useful to
Sonali.