I have this script in a .sh
file.
How can I transform it into a .bat
file?
if ! "$JAVA_CMD" -cp "$CP" "$MAIN_CLASS" "$@" 2>/dev/null; then
echo "error: main class is not found."
exit 1
fi
I have this script in a .sh
file.
How can I transform it into a .bat
file?
if ! "$JAVA_CMD" -cp "$CP" "$MAIN_CLASS" "$@" 2>/dev/null; then
echo "error: main class is not found."
exit 1
fi
This is roughly equivalent, although any non-zero return value would echo the same text.
@echo off
set CP="c:\mydir\MyClass.jar"
set MAIN_CLASS="MyClass"
java -cp %CP% %MAIN_CLASS% %* 2>NUL
IF %ERRORLEVEL% NEQ 0 ECHO "error: main class is not found."
You could echo the error output to a temp file and then just type the file on a non-zero return to display the true error:
@echo off
set CP="c:\mydir\MyClass.jar"
set MAIN_CLASS="MyClass"
set tempError=%temp%\javaRunError.txt
java -cp %CP% %MAIN_CLASS% %* 2>%tempError%
IF %ERRORLEVEL% NEQ 0 type %tempError%
If you want to get fancy, you can setup a goto and pass the exit (errorlevel) back to the caller.
@echo off
set CP="c:\mydir\MyClass.jar"
set MAIN_CLASS="MyClass"
set tempError=%temp%\javaRunError.txt
java -cp %CP% %MAIN_CLASS% %* 2>%tempError%
IF %ERRORLEVEL% NEQ 0 goto badExit
:: Good-Bye!
:end
echo.
echo.
echo %time% %0 Completed Successfully!
exit /b
:badExit
echo.
echo.
echo %time% Failure Occurred in %0...
type %tempError%
exit /b 1