gpt4 book ai didi

windows - 用于检查系统中是否存在 Java 主目录并检查 Java 版本的批处理脚本

转载 作者:可可西里 更新时间:2023-11-01 11:53:09 25 4
gpt4 key购买 nike

有没有办法写一个批处理脚本来检查环境中是否存在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。但是它没有给出任何正确的输出

最佳答案

编辑 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

关于windows - 用于检查系统中是否存在 Java 主目录并检查 Java 版本的批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com