Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exit status of a Java program in Windows batch file

Analogous to the $? in Linux, is there a way to get the exit status of a program in a Windows batch file (.bat)?
Say for example the program has a System.exit(0) upon successful execution, and a System.exit(1) upon a failure, how do I trap these exit values in a .bat file?

like image 345
pugmarx Avatar asked Sep 10 '25 06:09

pugmarx


1 Answers

Something like:

java Foo
set exitcode=%ERRORLEVEL%
echo %exitcode%

It's important to make this the absolute next line of the batch file, to avoid the error level being overwritten :)

Note that if you use the

IF ERRORLEVEL number

"feature" of batch files, it means "if the error level is greater than or equal to number" - it's not based on equality. I've been bitten by that before now :)

like image 186
Jon Skeet Avatar answered Sep 12 '25 19:09

Jon Skeet