在Windows 7registry中查找Java CurrentVersion

我支持一个旧的Java客户端应用程序,在Windows XP上运行正常,但不支持Windows 7 32位。 问题出在用于启动应用程序的BAT文件中。 BAT文件包含查询registry以查找Java的CurrentVersion的代码,然后使用该值确定用户计算机上该Java版本的path。 在Java客户端应用程序启动时,为了将rt.jar包含在CLASSPATH中,这是必需的。 下面的代码在Windows XP上工作正常,但在Windows 7上它返回:

找不到“HKLM \ SOFTWARE \ JavaSoft \ Java Runtime Environment”\ CurrentVersion。

:find_java setlocal ENABLEEXTENSIONS set KEY_NAME="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" set VALUE_NAME=CurrentVersion :: :: get the current version :: FOR /F "usebackq skip=4 tokens=3" %%A IN (`REG QUERY "HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" /v CurrentVersion 2^>nul`) DO ( set ValueValue=%%A ) if defined ValueValue ( @echo the current Java runtime is %ValueValue% ) else ( @echo %KEY_NAME%\%VALUE_NAME% not found. goto return ) set JAVA_CURRENT="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%ValueValue%" set JAVA_HOME=JavaHome :: :: get the javahome :: FOR /F "usebackq skip=4 tokens=3,4" %%A IN (`REG QUERY %JAVA_CURRENT% /v %JAVA_HOME% 2^>nul`) DO ( set JAVA_PATH=%%A %%B ) set JAVA_PATH=%JAVA_PATH:Program Files=Progra~1% echo using %JAVA_PATH% set JAVA_HOME=%JAVA_PATH% echo. :return goto start 

当涉及到复杂的DOS命令编程时,我不是很懂技术。 任何线索我如何更新此代码,以便它可以在Windows 7(32位)下工作?

也许这可能有帮助

 @echo off setlocal enableextensions disabledelayedexpansion :: possible locations under HKLM\SOFTWARE of JavaSoft registry data set "javaNativeVersion=" set "java32ON64=Wow6432Node\" :: for variables :: %%k = HKLM\SOFTWARE subkeys where to search for JavaSoft key :: %%j = full path of "Java Runtime Environment" key under %%k :: %%v = current java version :: %%e = path to java set "javaDir=" set "javaVersion=" for %%k in ( "%javaNativeVersion%" "%java32ON64%") do if not defined javaDir ( for %%j in ( "HKLM\SOFTWARE\%%~kJavaSoft\Java Runtime Environment" ) do for /f "tokens=3" %%v in ( 'reg query "%%~j" /v "CurrentVersion" 2^>nul ^| find /i "CurrentVersion"' ) do for /f "tokens=2,*" %%d in ( 'reg query "%%~j\%%v" /v "JavaHome" 2^>nul ^| find /i "JavaHome"' ) do ( set "javaDir=%%~e" & set "javaVersion=%%v" ) ) if not defined javaDir ( echo Java not found ) else ( echo JAVA_HOME="%javaDir%" echo JAVA_VERSION="%javaVersion%" ) endlocal