-1

I am executing a batch file which checks if Java\jre6 present then one type of command will be executed , then at last an else statement, but I find all stmts getting executed together.

echo.
echo. 
GOTO:Java

:Java
IF EXIST C:\Program Files\Java\jre6(
echo. Java Version 1.6 present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 6 thereeee"

if "%PROCESSOR_ARCHITECTURE%"=="x86" (
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre6\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre6\lib\ext\" /s /Y
)
else(
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\ext\" /s /Y
)
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 6"
)

IF EXIST C:\Program Files\Java\jre7(
echo. Java Version 1.7 present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 7 thereeee"
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre7\lib\ext\" /s /Y
XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles%\Java\jre7\lib\security\" /s /Y
)
else
(
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\ext\" /s /Y
XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\security\" /s /Y
)
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 7"
)

IF EXIST C:\Program Files\Java\jre8(
echo. Java Version 1.8 present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 8 thereeee"
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre8\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre8\lib\ext\" /s /Y
)
else(
XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\security\" /s /Y
XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\ext\" /s /Y
)
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 8"
) 
ELSE (
echo. Java NOT present
start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java is not Installed In your system , install Java 1.8 and try to execute settings again"
GOTO:EXIT
)

:EXIT
2
  • 1
    Your last ELSE doesn' thave a matching IF.
    – DavidPostill
    Jul 17, 2015 at 14:28
  • Also, try changing IF EXIST C:\Program Files\Java\jre6( to IF EXIST "C:\Program Files\Java\jre6" (, and do the same for the JRE7 and JRE8 IF statement. Jul 17, 2015 at 14:31

1 Answer 1

1

Critical Problems that must be fixed:

  • The IF EXIST filename must be quoted if it contains spaces (or poison characters like &).
  • There must be a space before ( when beginning a block of code
  • ), ELSE, and ( must all appear on the same line
  • Your logic is wrong - it will always execute "Java NOT present" code unless version 1.8 exists. You must either make the entire construct one big IF ... ELSE IF ... chain, or else GOTO :SomeLabel at the end of each IF block

Additional Suggestions:

  • ECHO. is not reliable. There are unusual circumstances where it can fail. Better to get in the habit of using ECHO(, as it always works. I know it looks "wrong", like it would confuse code blocks, but it is actually very reliable.
  • ECHO( is only needed when you have nothing other than white space (or the possibility of only whitespace) that is being ECHOed. But it certainly doesn't hurt to always use that form.
  • It is possible to have multiple JRE versions installed on the same machine. You might consider reversing the order so that it always prepares the most recent version found.
  • Unless you have additional code not shown near the top, the GOTO :JAVA is not needed
  • Unless you have additional code after :EXIT, then it is better to use GOTO :EOF or EXIT /B to simply exit your script, without any need for a label at the bottom.
  • Indentation is your friend. It helps to see the logic of your code - makes it much easier to understand.
echo(
echo( 
GOTO:Java

REM Presumably the GOTO above is conditional, and there is more code that goes here

:Java
IF EXIST "C:\Program Files\Java\jre6" (
  echo( Java Version 1.6 present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 6 thereeee"

  if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre6\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre6\lib\ext\" /s /Y
  ) else (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre6\lib\ext\" /s /Y
  )
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 6"
) else IF EXIST "C:\Program Files\Java\jre7" (
  echo( Java Version 1.7 present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 7 thereeee"
  if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre7\lib\ext\" /s /Y
    XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles%\Java\jre7\lib\security\" /s /Y
  ) else (
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\ext\" /s /Y
    XCOPY "%CD%\Sec7\*.*"  "%ProgramFiles(x86)%\Java\jre7\lib\security\" /s /Y
  )
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 7"
) else IF EXIST "C:\Program Files\Java\jre8" (
  echo( Java Version 1.8 present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java 8 thereeee"
  if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles%\Java\jre8\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles%\Java\jre8\lib\ext\" /s /Y
  ) else (
    XCOPY "%CD%\Sec\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\security\" /s /Y
    XCOPY "%CD%\Jar\*.*"  "%ProgramFiles(x86)%\Java\jre8\lib\ext\" /s /Y
  )
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Your Settings have been installed successfully for 8"
) ELSE (
  echo( Java NOT present
  start /wait /d "%CD%\Message" cscript MessageBox.vbs  "Java is not Installed In your system , install Java 1.8 and try to execute settings again"
  goto :EXIT
)
REM Presumably there is more code here

:EXIT
REM Presumably there is cleanup code here

There is plenty more I would do to improve the code, but that is enough for now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .