I have a batch script that executes a switch-case statement as shown in the accepted answer in switch case. After that, the script ends by calling a java program.
The script fails to call the java program when passing through any of the valid cases, but, when the control passes through the default case, the java program is successfully called. I surmise that the only difference between passing through the rest of the cases and the default case is CALL vs GOTO. But why is this happening? Is my assumption right? If so, how do I bypass this? Thank You.
setlocal enabledelayedexpansion set "defcase=0" set arg1=%1 shift set args= :args_loop if "%~1" == "" GOTO after_args_loop set args=%args%%1 shift GOTO args_loop :after_args_loop 2>NUL CALL :CASE_%COMMAND% if %defcase% == 0 (GOTO :DEFAULT_CASE) ELSE (GOTO :EOF) GOTO :DEFAULT_CASE :CASE_aaaa ... GOTO ENDSWITCH :CASE_bbbb ... GOTO ENDSWITCH rem multiple cases :DEFAULT_CASE ... set "defcase=1" GOTO ENDSWITCH :ENDSWITCH if %defcase%==0 set args=%arg1% %args% :: The JAVA program I want to call %a% %b% -cp %c% %d% %args% set "defcase=1"
If I instead of CALL, use GOTO for the switch statement, like this:
goto ENDSWITCH :CASE_aaa ... GOTO ENDSWITCH :CASE_bbb ... :ENDSWITCH
All the cases call the respective java programs, but the default case terminates after the () and does not exec GOTO ENDSWITCH.
The script is called like this:
file.bat command argsforjavaprog
command - used in switch (%command% in script) to identify which java program to call (from a list). If the entered argument does not match any existing java programs, a default program is called in default case.
argsforjavaprog - arguments passed during the call to the java program