acimutal
acimutal

Reputation: 2255

How to evaluate exit code of an application started from within a batch file?

I am trying to execute an application from a batch file by using START command (to hide console when executing) and I need to get errorlevel after execution. For example after System.exit(10) I'd like to restart Java application:

:while
START javaw ...
IF errorlevel 10 GOTO while     

But it doesn't work because errorlevel condition is evaluated before the java process has finished.

I've also tested the next code but I get an empty text file because of the same reason:

:while
start javaw ... >exit.txt 2>&1
set /p status=<exit.txt
if "%status%"=="10" goto :while  

Then, is there any way to launch a Java application, without console (/WAIT is not an option), and using a loop to restart the app when a problem occurs?

Upvotes: 1

Views: 883

Answers (2)

Mofi
Mofi

Reputation: 49206

See answer on question How to call a batch file in the parent folder of current batch file? to understand the 4 different methods on running an application or batch file from within a batch file.

And see also Difference between java/javaw/javaws.

The execution of the batch file results already in opening a console window.

Therefore I suggest to use simply

:while
java.exe ... 
IF errorlevel 10 GOTO while

Or use following code if you don't want to see any output by the Java application in console window of the batch file:

:while
java.exe ... 1>nul 2>nul
IF errorlevel 10 GOTO while

1>nul redirects output written to stdout to device NUL and 2>nul redirects the error messages written to stderr also to device NUL.

The only solution I can think of running a Java application without displaying a console window and additionally check successful execution of the Java application would be to write a Windows (GUI) application in C/C++/C# which does not open a window like javaw.exe and which runs javaw.exe with the appropriate parameters as process with evaluating the return code.

But with a batch file it is impossible to avoid opening a console window completely as far as I know. The console window can be opened minimized, but not hidden completely.

Upvotes: 1

DTSCode
DTSCode

Reputation: 1100

using this solution: Windows batch assign output of a program to a variable you can do something like this:

@echo off
:loop
application arg0 arg1 > temp.txt
set /p status=<temp.txt
if "%status%"=="10" goto :loop
echo "Done."

Upvotes: 0

Related Questions