@echo off
:findjdk64
cd %windir%\sysnative 2>NUL
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Development Kit" /v CurrentVersion 2^>NUL') do set javaver=%%B
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Development Kit\%javaver%" /v JavaHome 2^>NUL') do set javahome=%%B
if "%javahome%"=="" goto findjre64
echo %javahome%
goto end
:findjre64
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion 2^>NUL') do set javaver=%%B
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Runtime Environment\%javaver%" /v JavaHome 2^>NUL') do set javahome=%%B
if "%javahome%"=="" goto findjdk32
echo %javahome%
goto end
:findjdk32
cd %windir% 2>NUL
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Development Kit" /v CurrentVersion 2^>NUL') do set javaver=%%B
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Development Kit\%javaver%" /v JavaHome 2^>NUL') do set javahome=%%B
if "%javahome%"=="" goto findjre32
echo %javahome%
goto end
:findjre32
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion 2^>NUL') do set javaver=%%B
for /f "skip=2 tokens=2*" %%A in ('reg query "HKLM\Software\JavaSoft\Java Runtime Environment\%javaver%" /v JavaHome 2^>NUL') do set javahome=%%B
if "%javahome%"=="" goto end
echo %javahome%
:end