English 中文(简体)
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_MACHINESOFTWAREJavaSoftJava 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
最佳回答

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_MACHINESOFTWAREWOW6432NodeJavaSoftJava Development Kit"
TYPE reg32.txt | FIND "JavaHome" > merged.txt
START /w REGEDIT /e reg64.txt "HKEY_LOCAL_MACHINESOFTWAREJavaSoftJava 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
问题回答

You could check the entries under HKEY_LOCAL_MACHINESOFTWAREJavaSoftJava Runtime Environment in the registry

Do you really need to use the Windows registry? If not, you can check for the existence of the JAVA_HOME and/or JDK_HOME environment variables. If they exist, then they point to the installation directory of the Java install, and you can probably assume that the Java installation they point to is the one the user wants to use.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签