65

How within a batch file to check if command

start "" javaw -jar %~p0/example.jar

was successful or produced an error?

I want to use if/else statements to echo this info out.

Joey
  • 344,408
  • 85
  • 689
  • 683
31415926
  • 3,811
  • 7
  • 47
  • 78

6 Answers6

88

This likely doesn't work with start, as that starts a new window, but to answer your question:

If the command returns a error level you can check the following ways

By Specific Error Level

commandhere
if %errorlevel%==131 echo do something

By If Any Error

commandhere || echo what to do if error level ISN'T 0

By If No Error

commandhere && echo what to do if error level IS 0

If it does not return a error level but does give output, you can catch it in a variable and determine by the output, example (note the tokens and delims are just examples and would likely fail with any special characters)

By Parsing Full Output

for /f "tokens=* delims=" %%a in ('somecommand') do set output=%%a
if %output%==whateveritwouldsayinerror echo error

Or you could just look for a single phrase in the output like the word Error

By Checking For String

commandhere | find "Error" || echo There was no error!
commandhere | find "Error" && echo There was an error!

And you could even mix together (just remember to escape | with ^| if in a for statement)

Hope this helps.

Patrick Meinecke
  • 3,963
  • 2
  • 18
  • 26
52

You can use

if errorlevel 1 echo Unsuccessful

in some cases. This depends on the last command returning a proper exit code. You won't be able to tell that there is anything wrong if your program returns normally even if there was an abnormal condition.

Caution with programs like Robocopy, which require a more nuanced approach, as the error level returned from that is a bitmask which contains more than just a boolean information and the actual success code is, AFAIK, 3.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 4
    This is the right answer as it is more explicit than checking `%errorlevel%` as [ERRORLEVEL is not %ERRORLEVEL%](https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743). – sschuberth Apr 26 '17 at 08:11
  • 1
    @TomášZato: It will trigger on any positive exit code. – Joey Sep 24 '20 at 19:33
  • @sschuberth That link is now broken, updated link: https://devblogs.microsoft.com/oldnewthing/20080926-00/?p=20743 – Max Barraclough Nov 02 '20 at 19:51
  • @sschuberth what if errorlevel is -1? – Marco Sulla Dec 27 '21 at 17:33
  • What about it @marco-sulla? – sschuberth Dec 27 '21 at 21:32
  • @sschuberth Well, `if errorlevel 1` does not work, since it checks only if errorlevel is greater than 1. IMHO the only safe way to check if there's an error is to check if errorlevel is not equal to zero. – Marco Sulla Dec 29 '21 at 07:14
  • Indeed, I just verified that the `errorlevel` is signed on Windows, and an `exit /b -1` does *not* result in `errorlevel` to be 255 (like it would have been the case in DOS times), but -1. Anyway, the point of this answer was to use `errorlevel` instead of `%errorlevel%`, and the comparison was just an example IMO. – sschuberth Dec 29 '21 at 11:27
19

Most commands/programs return a 0 on success and some other value, called errorlevel, to signal an error.

You can check for this in you batch for example by:

call <THE_COMMAND_HERE>
if %ERRORLEVEL% == 0 goto :next
echo "Errors encountered during execution.  Exited with status: %errorlevel%"
goto :endofscript

:next
echo "Doing the next thing"

:endofscript
echo "Script complete"
admdrew
  • 3,790
  • 4
  • 27
  • 39
jsears
  • 4,511
  • 2
  • 31
  • 36
8

Goodness I had a hard time finding the answer to this... Here it is:

cd thisDoesntExist
if %errorlevel% == 0 (
  echo Oh, I guess it does
  echo Huh.
)
Andrew
  • 5,839
  • 1
  • 51
  • 72
2

I don't know if javaw will write to the %errorlevel% variable, but it might.

echo %errorlevel% after you run it directly to see.

Other than that, you can pipe the output of javaw to a file, then use find to see what the results were. Without knowing the output of it, I can't really help you with that.

Gray
  • 7,050
  • 2
  • 29
  • 52
0

So logically, this would test for success:

if not errorlevel 1 echo success

Well, unless there's a negative exit code, which would be unusual. This also has the bonus of seeing what the errorlevel is.

if %errorlevel%==0 echo success
js2010
  • 23,033
  • 6
  • 64
  • 66