Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Java is installed via REGEDIT.exe from a batch file?

I am looking for a batch file snippet that somehow reads the Windows registry and detects which Java JDK is on a Windows system and then asks the user which one they want to use and remembers the choice.

Here is what I have so far... needs some modifications. This script only finds the first JDK... it doesn't handle multiples.

@echo off
SETLOCAL EnableDelayedExpansion
:: findJDK.bat
start /w regedit /e reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit" 
type reg1.txt | find "JavaHome" > reg2.txt 
if errorlevel 1 goto ERROR 
for /f "tokens=2 delims==" %%x in (reg2.txt) do (
  set JavaTemp=%%~x
  echo Regedit: JAVA_HOME path : !JavaTemp!
)
if errorlevel 1 goto ERROR
echo.
set JAVA_HOME=%JavaTemp%
set JAVA_HOME=%JAVA_HOME:\\=\%
echo JAVA_HOME was found to be %JAVA_HOME%
goto END
:ERROR
echo reg1.txt is: & type reg1.txt
echo reg2.txt is: & type reg2.txt
echo
:END
del reg2.txt
del reg1.txt
pause>nul
like image 960
djangofan Avatar asked Oct 15 '22 11:10

djangofan


2 Answers

You could check the entries under HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment in the registry

like image 111
Kannan Ekanath Avatar answered Oct 18 '22 13:10

Kannan Ekanath


Ok, I finally figured out the answer. It took a while, and this code is really rough, but it works. If someone wants to improve it, I will award them the answer points:

@ECHO off
SET TITLE=detectJDK.bat
TITLE=%TITLE%
SETLOCAL ENABLEDELAYEDEXPANSION
IF EXIST jdkhome.txt (
  ECHO JDK home already set in jdkhome.txt file.
  GOTO :END
)
ECHO. 2>merged.txt
ECHO. 2>list.txt
ECHO. 2>uniquelist.txt
IF NOT EXIST reg32.txt ECHO. 2>reg32.txt
IF NOT EXIST reg64.txt ECHO. 2>reg64.txt
START /w REGEDIT /e reg32.txt "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\JavaSoft\Java Development Kit"
TYPE reg32.txt | FIND "JavaHome" > merged.txt
START /w REGEDIT /e reg64.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
TYPE reg64.txt | FIND "JavaHome" >> merged.txt
FOR /f "tokens=2 delims==" %%x IN (merged.txt) DO (
  CALL :STRIP "%%~x" >>list.txt
)

FOR /F "tokens=* delims= " %%a IN (list.txt) DO (
  SET str=%%a
  FIND /I ^"!str!^" list.txt>nul
  FIND /I ^"!str!^" uniquelist.txt>nul
  IF ERRORLEVEL 1 ECHO !str!>>uniquelist.txt
)

ECHO Select a JDK from the list:
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%y IN (uniquelist.txt) DO (
  SET /A COUNT += 1
  ECHO    !COUNT!: %%~y
)
SET /P NUMBER=Type a number here: 
SET /A COUNT=0
FOR /f "tokens=1,* delims=" %%z IN (uniquelist.txt) DO (
  SET /A COUNT += 1
  IF !COUNT!==!NUMBER! (
    SET JAVA_HOME=%%~z
  )
)
ECHO.
ECHO JAVA_HOME was found to be %JAVA_HOME%
ECHO %JAVA_HOME%>jdkhome.txt
GOTO CLEANUP

:CLEANUP
DEL /Q merged.txt
DEL /Q list.txt
DEL /Q uniquelist.txt
DEL /Q reg32.txt
DEL /Q reg64.txt
GOTO :END

:: Strip quotes and extra backslash from string
:STRIP
SET n=%~1
SET n=%n:\\=\%
SET n=%n:"=%
IF NOT "%n%"=="" ECHO %n%
GOTO :EOF

:END
FOR /l %%a in (5,-1,1) do (TITLE %TITLE% -- Closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)
:: end
like image 24
djangofan Avatar answered Oct 18 '22 13:10

djangofan