npocmaka / batch.scripts

batch script utils and examples by npocmaka -
https://www.linkedin.com/in/npocmaka/
MIT License
1.07k stars 874 forks source link

Fix Suggestion #44

Open sebetci opened 2 years ago

sebetci commented 2 years ago

When the javaDetect.bat script is run after a new CMD session is opened, the following error occurs:

Environment variable JAVA_VERSION not defined

To fix this error, the comment lines assigning to the JAVA_VERSION variable have been removed.

Click to see the code.

```bash @ECHO OFF SET __COMPAT_LAYER=RunAsInvoker :: Export java settings from registry to a temporary file START /W REGEDIT /E %Temp%\java.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft" IF NOT EXIST "%Temp%\java.reg" ( START /W REGEDIT /E %Temp%\java.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft" ) IF NOT EXIST "%Temp%\java.reg" ( ECHO Java NOT installed EXIT /b 1 ) :: Find java location FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%\java.reg ^| FIND "INSTALLDIR"') DO SET JAVA_HOME=%%B SET JAVA_HOME=%JAVA_HOME:"=% SET JAVA_HOME=%JAVA_HOME:\\=\% SET JAVA_HOME :: Get java version FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%\java.reg ^| FIND "CurrentVersion"') DO SET JAVA_VERSION=%%B SET JAVA_VERSION=%JAVA_VERSION:"=% SET JAVA_VERSION SET JAVA_VERSION=%JAVA_VERSION:.=% SET JAVA_VERSION=%JAVA_VERSION:_=% SET /A JAVA_VERSION=%JAVA_VERSION% FOR /f tokens^=2-5^ delims^=.-_^" %%j in ('%JAVA_HOME%\bin\java.exe -fullversion 2^>^&1') do set "JAVA_VERSION=%%j%%k%%l%%m" :: Delete temporary file DEL %Temp%\java.reg /S /Q > NUL 2>&1 set JAVA_VERSION :: Check java version compatibility IF %JAVA_VERSION% LSS 16020 ( ECHO. ECHO YOU NEED AT LEAST JAVA WITH VERSION 1.6.0_20 -- this is just an example echo. GOTO :EOF ) PAUSE ```