Discover from a batch file where is Java installed

2020-07-03 09:12发布

I want to set the JAVA_HOME variable from a batch script

9条回答
\"骚年 ilove
2楼-- · 2020-07-03 09:37

You can use values stored in the registry to automatically discover where Java is installed and setup the JAVA_HOME variable.

HKLM > Software > JavaSoft > Java Runtime Environment

At this location is a Key called CurrentVersion. This version references one of the directories at this level by name. Opening the directory exposes another key called JavaHome. The value of JavaHome is a file system path that can be used to define your environment variable JAVA_HOME.

Within a batch file you can do something like:

FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion') DO set CurVer=%%B

FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment\%CurVer%" /v JavaHome') DO set JAVA_HOME=%%B

If you want to read more, I've written a tutorial describing what is needed to construct a batch file to auto-discover JAVA_HOME.

查看更多
家丑人穷心不美
3楼-- · 2020-07-03 09:39

If JAVA_HOME isn't already set, and you want to set it, then you probably need to do something like

dir java.exe /B /S

which will give you a list of all directories containing the file java.exe. From there you can pipe that output to another command that parses the results and selects one of those directories, then uses it to set the JAVA_HOME variable.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-07-03 09:46

This solution depends on the JDK being installed under %ProgramFiles%\Java, for example C:\Program Files\Java\jdk1.6.0_18. You can change the line "set JDK_Version=1.6" to the version you want to use such as "set JDK_Version=1.5".

Assuming that the latest version of JDK would be at the bottom of the list (jdk%jdk_Version%*) the latest version available should be set as JAVA_HOME. If the JDK could not be found JAVA_HOME will not be changed. If the JDK could not be found and JAVA_HOME doesn't have a value the script will display an error message.

@echo off
rem set the version of jdk you would like to use (1.4, 1.5, 1.6, etc)
set JDK_Version=1.6

echo.
echo Locating JDK %JDK_Version%

for /d %%i in ("%ProgramFiles%\Java\jdk%jdk_Version%*") do (set Located=%%i)
rem check if JDK was located
if "%Located%"=="" goto else
rem if JDK located display message to user
rem update %JAVA_HOME%
set JAVA_HOME=%Located%
echo     Located JDK %jdk_Version%
echo     JAVA_HOME has been set to:
echo         %JAVA_HOME%
goto endif

:else
rem if JDK was not located
rem if %JAVA_HOME% has been defined then use the existing value
echo     Could not locate JDK %JDK_Version%
if "%JAVA_HOME%"=="" goto NoExistingJavaHome
echo     Existing value of JAVA_HOME will be used:
echo         %JAVA_HOME%
goto endif

:NoExistingJavaHome
rem display message to the user that %JAVA_HOME% is not available
echo     No Existing value of JAVA_HOME is available
goto endif

:endif
rem clear the variables used by this script
set JDK_Version=
set Located=
查看更多
小情绪 Triste *
5楼-- · 2020-07-03 09:47

Here is a handy little batch file that will search everything in your path for the first occurance of a file. The magical little bit is the %%~dp$PATH:i%%i in the for loop. %%~dp will expand the next variable to the drive letter and path only. The $PATH:i searches the path for the first occurance of %%i which is the filename that is passed into the for loop.

example: Where.bat

@echo off
setlocal

if "%1"=="" goto USAGE
set filename=%1

for %%i in (%filename%) do @echo %%~dp$PATH:i%%i

goto EOF

:USAGE

echo %0 filename

:EOF
endlocal
查看更多
ら.Afraid
6楼-- · 2020-07-03 09:48

See Get the current Java version from a BAT file to get the current Java installation based on the infos stored in the registry.

查看更多
一夜七次
7楼-- · 2020-07-03 09:52

Java's supposed to set up the JAVA_HOME variable when it's installed. Use this to find the location of Java:

if not "%JAVA_HOME%" == "" goto HasJavaHome

:HasJavaHome
set JAVA_HOME=... // Your new value goes here.
查看更多
登录 后发表回答