-1

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  
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Fariba
  • 693
  • 1
  • 12
  • 27
  • 1
    You want to write the same exact content of the .sh file to a .bat file? If yes, it probably won't work on Windows since Windows doesn't execute shell code the same way as Linux. – cyber_rookie Apr 15 '15 at 12:59
  • I would like to change it to format acceptable in windows – Fariba Apr 15 '15 at 13:00
  • you need to change the content because both supports different - different commands. – Prashant Apr 15 '15 at 13:01
  • how can i change it? – Fariba Apr 15 '15 at 13:03
  • @faribafarrokhpour that depends a lot on what you are trying to achieve. This link seems to have good info: http://www.csie.ntu.edu.tw/~r92092/ref/win32/win32scripting.html – cyber_rookie Apr 15 '15 at 13:03
  • @cyber_rookie - he's trying to port the script to batch. That's all. – SomethingDark Apr 15 '15 at 13:35
  • @SomethingDark im not aware of porting linux scripts to windows batch files. I just suggested he check what might be the batch equivalents for shell directives – cyber_rookie Apr 15 '15 at 13:37
  • thanks, I am searching batch equivalents for this code – Fariba Apr 15 '15 at 13:42
  • possible duplicate of [How to run java application by .bat file](http://stackoverflow.com/questions/8938944/how-to-run-java-application-by-bat-file) – nana Apr 16 '15 at 13:31

1 Answers1

2

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
Mike
  • 3,186
  • 3
  • 26
  • 32