Auto-Discover JAVA_HOME on Windows Jun 15

It's bothered me for a while now that many open source distributions and software packages include batch files that depend on JAVA_HOME being defined and pointing to the correct location. There must be a way around this dependency since people may not have the JAVA_HOME environment variable defined properly. The following is a small tutorial describing a method for automatically discovering JAVA_HOME on a user's machine from a batch file. This may be useful for anyone wanting to setup JAVA_HOME automatically before starting their java application.

After doing a bit of research I discovered that Java, during its installation, creates several useful registry entries. The section of interest in this case is:

HKLM > Software > JavaSoft > Java Runtime Environment

Within the 'Java Runtime Environment' are several registry keys along with directories for each version of Java that has been installed on the user's machine. Among the keys includes one called CurrentVersion. This key points to the current version of Java that has been installed on the machine. By this I mean it references one of the directories under 'Java Runtime Environment' by name. That directory contains a key called JavaHome. The value of this key is a filesystem path to an installation of Java.

Now that I have found a way to determine the current version of Java installed on the user's machine and the path to that installation there must be a way to write it up within a batch file to automatically discover JAVA_HOME if it is undefined.

The batch command that accomplishes just this is 'FOR' which is able to execute a command and store its return in a variable. Combining 'FOR' with 'REG QUERY' allows registry keys to be queried for their values, parses the response and stores the result in a variable.

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

Now taking advantage of those two commands we can put together a simple batch file that checks the contents of JAVA_HOME and if empty runs the automatic discovery.

@echo off
if "%JAVA_HOME%"=="" call:FIND_JAVA_HOME
echo "Java home: %JAVA_HOME%"
rem Start your java application or perform other actions here
goto:END

:FIND_JAVA_HOME
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
goto:EOF

:END

My batch scripting knowledge is limited so there may be more efficient ways of writing this script. If that is the case please comment.


tags: batch java script tutorial

Comments

Peter Jun 17

Nice tutorial. This is just what I've been trying to do.