- 1 year ago
Trouble getting errorlevel from executed Java
Comment
1 / 1
When executing a Java program from a batch script or command line, you can check the errorlevel
to determine the exit status of the Java program. However, the errorlevel
value might not be set correctly when running a Java program directly using the java
command.
To overcome this issue, you can use the cmd /c
command to execute the Java program from a batch script. Here's an example:
batch@echo off
set "javaPath=C:\path\to\java.exe"
set "jarPath=C:\path\to\your\program.jar"
cmd /c "%javaPath%" -jar "%jarPath%"
if %errorlevel% equ 0 (
echo Java program executed successfully.
) else (
echo Error executing Java program.
)
In this example:
- Set the
javaPath
variable to the path of thejava.exe
executable. - Set the
jarPath
variable to the path of your Java program's JAR file. - The
cmd /c
command is used to execute the Java program using thejava
command within thecmd
shell. - After executing the Java program, the
errorlevel
is checked using anif
statement. If theerrorlevel
is 0, it means the Java program executed successfully. Otherwise, an error occurred.
By using the cmd /c
command, you can capture the correct errorlevel
value from the Java program execution and perform further actions based on the result.
Make sure to adjust the paths and filenames in the script to match your specific environment and Java program.