How within a batch file to check if command
start \"\" javaw -jar %~p0/example.jar
was successful or produced an error?
I want to u
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.
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.
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.
)
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"
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.