Discover from a batch file where is Java installed?
Solution 1
This snippet will search the current PATH for java.exe, and print out where it was found:
for /f %%j in ("java.exe") do @echo.%%~dp$PATH:j
On my system this gives me
C:\WINDOWS\system32\
Using this you can set JAVA_HOME as follows:
@echo off
for /f %%j in ("java.exe") do (
set JAVA_HOME=%%~dp$PATH:j
)
if %JAVA_HOME%.==. (
@echo java.exe not found
) else (
@echo JAVA_HOME = %JAVA_HOME%
)
Solution 2
See Get the current Java version from a BAT file to get the current Java installation based on the infos stored in the registry.
Solution 3
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=
Solution 4
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.
flybywire
Updated on July 10, 2022Comments
-
flybywire almost 2 years
I want to set the JAVA_HOME variable from a batch script
-
flybywire about 15 yearsi want to "discover" where java is.
-
eleven81 about 15 yearsThis will only work run from the directory where java is installed, or from any superdirectory thereof. It will not work, for instance, if run from C:\ while java is installed somewhere on D:\
-
McDowell about 15 yearssystem32 is not where the JRE will be installed. See mindprod.com/jgloss/registry.html#JAVAFIND JAVA_HOME is typically used in scripts in this form to launch apps: %JAVA_HOME%\bin\java
-
McDowell about 15 yearsJava has never set the JAVA_HOME variable when installed. It is a convention used to allow the script-writer to allow the system user to specify a specific version of Java to use when launching an app, since there can be more than one version installed.
-
Patrick Cuff about 15 yearsI'm not saying it is, but that's where it happens to reside in my path. From another answer (or comments to another answer, I don't know, it's been deleted) you may have an installer that copies java.exe to the %systemroot%\system32 directory. On my system, it apparently has.
-
codewarrior over 12 yearsI need to find out where java.exe is without relying on its folder being in PATH (it usually isn't....). I have to use the registry setting from the batch file in the answer below this one.
-
Jayy about 9 yearsCan you pls explain on %%~dp$PATH:j
-
Patrick Cuff about 9 years@Jayy; it's from the
for
command help,for /?
: %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found. -
Berit Larsen over 8 years"set JAVA_HOME=%%~dp$PATH:j" "The syntax of the command is incorrect"
-
mishal153 over 8 yearswhat is %JAVA_HOME%.==. doing exactly ? is this the way to check for empty strings ?
-
Patrick Cuff over 8 years@mishal153, yes, this is checking for empty strings. BAT files treat whitespace oddly, so there's no easy way to check for empty strings. By putting a period at the end of a string and comparing to a literal that's just a period, you can tell if the string is empty.
-
isapir about 8 years@PatrickCuff - Nice solution!
JAVA_HOME
is the parent directory of the directory that containsjava.exe
though, so you should traverse one directory up from the result found.java.exe
is expected to be found at%JAVA_HOME%\bin\java.exe