有没有办法编写一个批处理脚本来检查环境中是否存在java home并检查java版本是否高于7
@echo off
IF "%JAVA_HOME%" == "" (
echo Enter path to JAVA_HOME:
set /p JAVA_HOME=
) ELSE (
echo %JAVA_HOME%
)
我使用上面的脚本来检查java home.But它没有提供任何正确的输出
答案 0 :(得分:2)
已编辑 2017/11/16以适应new JRE registry location。行下的原始答案
@echo off
setlocal enableextensions disabledelayedexpansion
:: for variables
:: %%L = possible locations under HKLM\SOFTWARE of JavaSoft registry data
:: note: the list includes native and redirected locations
:: %%J = possible locations under %%L where to find the JRE information
:: note: the list is sorted from newer to older locations
:: %%k = The generated JRE root key to check
:: %%v = current java version
:: %%e = path to java
for %%L in (
"" "Wow6432Node\"
) do for %%J in (
"JRE" "Java Runtime Environment"
) do if not defined javaDir (
for %%k in (
"HKLM\SOFTWARE\%%~LJavaSoft\%%~J"
) do for /f "tokens=3" %%v in (
'reg query "%%~k" /v "CurrentVersion" 2^>nul ^| find /i "CurrentVersion"'
) do for /f "tokens=2,*" %%d in (
'reg query "%%~k\%%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%"
)
pause
原始答案
if defined JAVA_HOME (
echo JAVA_HOME="%JAVA_HOME%"
) else (
echo JAVA_HOME is not defined
)
但是这不会告诉你(也不会问用户)是否安装了java或它是什么版本。
如果安装了java,你可以问windows(它存储在注册表中)它在哪里以及你有什么版本
@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
pause