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.
To test for a specific ERRORLEVEL, use an IF command with the %ERRORLEVEL% variable. n.b. Some errors may return a negative number. If the batch file is being executed as a scheduled task, then exiting with an error code will be logged as a failed task. You can monitor the event log to discover those failures.
[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.
Represents a replaceable parameter. Use a single percent sign ( % ) to carry out the for command at the command prompt. Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> )
EXIT /B at the end of the batch file will stop execution of a batch file. Use EXIT /B < exitcodes > at the end of the batch file to return custom return codes. Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file, which is the latest error codes from the last command executed.
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.
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.
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"
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.
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With