找到安装的JRE版本,如果找不到则安装它

时间:2015-02-18 20:17:45

标签: batch-file

我正在研究在注册表中检查Java运行时路径的子例程,如果找不到,则尝试安装它。我的代码在某个地方出错,但是我已经盯着它看太久而无法找到它。我运行时获得的输出是:

Running Microsoft Windows XP Service Pack 3
Installing HermesJMS.
Checking Java Runtime install path.
The syntax of the command is incorrect.

我非常确定错误位于:FindJRE子例程中。任何帮助非常感谢。这是我的代码。

::@echo off
setlocal

call :GetOSVer os sp
Echo Running %os% %sp%
call :sleep 2
set key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
ECHO Installing HermesJMS.
call :sleep 2
::hermes-installer-1.14.jar
echo Checking Java Runtime install path.
call :sleep 2
call :FindJRE jre
call :sleep 2
echo Setting Java_Home path.
call :sleep 2
reg add %key% /v JAVA_HOME /t REG_SZ /d %jre% /f>nul
For /f "tokens=3 delims=    " %%a in ('reg query %key% /v JAVA_HOME^|Find "JAVA_HOME"') do (
     Echo Java home path set to: %%a.
)
call :sleep 2
echo Checking for Capitalware...
call :sleep 2
if not exist "C:\Capitalware" (
    echo Installing Capitalware
    "%~dp0Capitalware\Windows\mqce-setup.exe"
)   
call :sleep 2
Echo You can now run HermesJMS.
call :sleep 5
pause
exit /b

:Sleep
@echo off & setlocal
Ping -n %1 127.0.0.1 >nul
exit /b

:GetOSVer
@echo off
set key="HKLM\Software\Microsoft\Windows NT\CurrentVersion"
for /f "skip=1 tokens=3 delims= " %%a in (
  'reg query %key% /v ProductName') do (
   set "%~1=%%a"
)
for /f "skip=1 tokens=3 delims= " %%a in (
  'reg query %key% /v CSDVersion') do (
   set "%~2=%%a"
)
exit /b

:FindJRE
::@echo off
setlocal
set KEY_NAME="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=2 tokens=3" %%A IN (
  `REG QUERY %KEY_NAME% /v CurrentVersion 2^>nul`
  ) DO ( set Val=%%~A )
    if defined Val (
        echo.the current Java runtime is %Val%
    ) else ( 
        echo.%KEY_NAME%\%VALUE_NAME% not found.
            for /f %%a in ('dir /b jre*') do (
                Echo.Attempting to install Java Runtime Environment. 
                echo.Flipping the junkware bit
                REG add HKLM\SOFTWARE\JavaSoft /v Sponsors /t REG_SZ /d Disable > nul 
                set loc="%%a" 
                    If defined %loc% ( %loc% 
                    ) ELSE (
                        Echo.Java installer not found. Please download the latest Java Runtime Environment from the Oracle Website.
                        exit /b 1
                    )       
            )
    )
FOR /F "usebackq skip=2 tokens=3*" %%A IN (
  `REG QUERY %KEY_NAME%\%Val% /v JavaHome 2^>nul`
  ) DO ( 
  endlocal
  set %~1="%%~A %%~B"
)
exit /b 0  

更新版本 - 完全正常工作

:: HermesJMS install script ::
:: Author - Matt Williamson ::
:: Rev 1 -     02/13/15     ::
::::::::::::::::::::::::::::::

@echo off
setlocal

call :GetOSVer os sp
Echo Running %os% %sp%
call :sleep 2
set key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
ECHO Installing HermesJMS.
call :sleep 2
hermes-installer-1.14.jar
echo Checking Java install path.
call :sleep 2
call :FindJRE jre

If not %jre% EQU dec (
    call :sleep 2
    echo Setting Java home path.
    call :sleep 2
    reg add %key% /v JAVA_HOME /t REG_SZ /d %jre% /f >nul
        For /f "skip=4 tokens=3*" %%a in ('reg query %key% /v JAVA_HOME') do (
            Echo Java home path set to: %%a %%b
        )
)   ELSE ( Endlocal&echo You must install Java Runtime Environment for Hermes to work.&exit /b 1)   
call :sleep 2
echo Checking for Capitalware...
call :sleep 2
if not exist "C:\Capitalware" (
    echo Installing Capitalware
    "%~dp0Capitalware\Windows\mqce-setup.exe"
)   
call :sleep 5
echo Now running HermesJMS
"C:\Program Files\HermesJMS\bin\hermes.bat"
exit /b 0 

:Sleep
@echo off & setlocal
Ping -n %1 127.0.0.1 >nul
exit /b

:GetOSVer
@echo off
set key="HKLM\Software\Microsoft\Windows NT\CurrentVersion"
for /f "skip=4 tokens=3 delims= " %%a in (
  'reg query %key% /v ProductName') do (
   set "%~1=%%a"
)
for /f "skip=4 tokens=3 delims= " %%a in (
  'reg query %key% /v CSDVersion') do (
   set "%~2=%%a"
)
exit /b

:FindJRE
@echo off
setlocal enabledelayedexpansion
set "aux1= "
set KEY_NAME="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=4 tokens=3" %%A IN (
  `REG QUERY %KEY_NAME% /v CurrentVersion 2^>nul`
  ) DO ( set "Val=%%~A" )
    if defined Val (
        echo.the current Java runtime is !Val!
    ) else ( 
        echo.Java Runtime Environment not found.
            for /f %%a in ('dir /b jre*') do (
                Echo.Attempting to install Java Runtime Environment. 
                set "loc=%%a"
                    If defined loc ( 
                        echo.Flipping the junkware bit
                        REG add HKLM\SOFTWARE\JavaSoft /v Sponsors /t REG_SZ /d Disable /f>nul
                        Call :SpinnerEx&!loc!
                        Set Key2="HKLM\SOFTWARE\JavaSoft"
                            FOR /F "usebackq skip=4 tokens=3" %%G IN (
                                `REG QUERY !Key2! /v InstallStatus 2^>nul`) do (
                                    If "%%G" EQU "decline" (
                                        Echo JRE install cancelled
                                        Endlocal&Set %~1=dec&exit /b 1
                                    )
                            )

                    ) ELSE (
                        Echo.Java installer not found. Please download the latest Java Runtime Environment from the Oracle Website.
                        exit /b 1
                    )       
            )
    )
FOR /F "usebackq skip=4 tokens=3*" %%A IN (
  `REG QUERY %KEY_NAME%\%Val% /v JavaHome 2^>nul`
  ) DO ( 
  set "aux1=%%~A %%~B"
)
endlocal&set %~1="%aux1%"&exit /b 0  

:SpinnerEx
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

FOR /L %%n in (1,1,10) DO (
    call :spinner
    ping localhost -n 1 > nul
)
exit /b

:spinner
set /a "spinner=(spinner + 1) %% 4"
set "spinChars=\|/-"
<nul set /p ".=Waiting for JRE...!spinChars:~%spinner%,1!!CR!"
exit /b                 

1 个答案:

答案 0 :(得分:2)

你是对的,错误发生在:FindJRE子程序中。这是尝试纠正它(但我没有声称完整性):

  • @echo on有助于找到错误源代码行
  • read more关于enabledelayedexpansion并使用!var!代替%var%
  • echo REG add ...命令echo ed,因为我不想因为可理解的原因真的这样做:)。
  • echo !loc! detto(以及)
  • set "Val=%%~A"将值设置为变量以确保没有尾随空格的最佳做法
  • If defined loc (而非奇怪的If defined %loc% (
  • endlocal&set %~1="%aux1%"&exit /b 0技巧来设置输出(通过引用)变量:
    • 使用先前定义的辅助aux变量
    • 另一个确保没有尾随空格的技巧:set %~1="%aux1%"&符号字符包围
  • 注意输出"%aux1%"可能会导致" ",即"引号包围的空格
  • 对不起,如果有任何更正被遗忘,因此未列在此处......

代码:

:FindJRE
@echo on
setlocal enabledelayedexpansion
set "aux1= "
set KEY_NAME="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=2 tokens=3" %%A IN (
  `REG QUERY %KEY_NAME% /v CurrentVersion 2^>nul`
  ) DO ( set "Val=%%~A" )
    if defined Val (
        echo the current Java runtime is %Val%
    ) else ( 
        echo.%KEY_NAME%\%VALUE_NAME% not found.
            for /f %%a in ('dir /b jre*') do (
                Echo.Attempting to install Java Runtime Environment. 
                echo.Flipping the junkware bit
                echo REG add HKLM\SOFTWARE\JavaSoft /v Sponsors /t REG_SZ /d Disable > nul 
                set loc="%%a" 

                    If defined loc ( 
                      echo !loc! 
                    ) ELSE (
                        Echo.Java installer not found. Please download the latest Java Runtime Environment from the Oracle Website.
                        exit /b 1
                    )       
            )
    )
FOR /F "usebackq skip=2 tokens=3*" %%A IN (
  `REG QUERY %KEY_NAME%\%Val% /v JavaHome 2^>nul`
  ) DO ( 
  set "aux1=%%~A %%~B"
)
endlocal&set %~1="%aux1%"&exit /b 0