@ECHO OFF

SET regKey0=HKEY_LOCAL_MACHINE\SOFTWARE\EMSInstaller
SET regKey1=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\EMSInstaller

REG QUERY "%regKey0%" 1>NUL 2>&1
IF %ERRORLEVEL% == 0 (
    SET queryPath=%regKey0%
    GOTO EMSJRE
)

REG QUERY "%regKey1%" 1>NUL 2>&1
IF %ERRORLEVEL% == 0 (
    SET queryPath=%regKey1%
    GOTO EMSJRE
)

GOTO END


:EMSJRE
SET regKey="%queryPath%"
SET regVal=SetupPath
FOR /F "tokens=2*" %%i IN ('REG QUERY %regKey% /v %regVal%' ) DO (
    IF NOT "%%j" == "" (
        SET emsPath=%%j
    )	
)
IF "%emsPath%" == "" (
    GOTO END
)
SET javaVersion=1.6
SET "javaHome=%emsPath%\jre6"

CALL "%javaHome%\bin\java" -version
IF NOT %ERRORLEVEL% == 0 (
    GOTO SYSJRE
)

SET PATH=%javaHome%\bin;%PATH%
GOTO END


:SYSJRE
SET regKey2=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
SET regKey3=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment
SET regVal=CurrentVersion

REG QUERY "%regKey2%" /v %regVal% 1>NUL 2>&1
IF %ERRORLEVEL% == 0 (
    SET queryPath=%regKey2%
)

REG QUERY "%regKey3%" /v %regVal% 1>NUL 2>&1
IF %ERRORLEVEL% == 0 (
    SET queryPath=%regKey3%
)

SET regKey="%queryPath%"
SET regVal=CurrentVersion
FOR /F "tokens=2*" %%i IN ('REG QUERY %regKey% /v %regVal%' ) DO (
    IF NOT "%%j" == "" (
        SET javaVersion=%%j
    )	
)
IF "%javaVersion%" == "" (
    GOTO END
)

SET regKey="%queryPath%\%javaVersion%"
SET regVal=JAVAHome
REG QUERY %regKey% /v %regVal% 1>NUL 2>&1
IF NOT %ERRORLEVEL% == 0 (
    GOTO END
)

FOR /F "tokens=2*" %%i IN ('REG QUERY %regKey% /v %regVal%' ) DO (
    IF NOT "%%j" == "" (
        SET javaHome=%%j
    )	
)
IF "%javaHome%" == "" (
    GOTO END
)

SET PATH=%javaHome%\bin;%PATH%
GOTO END

:END
