Finding and Setting JAVA HOME
Finding and Setting JAVA HOME
Depending on your operating environment, finding the correct value for and then setting the
JAVA_HOME environment variable can be either trivially easy or frustratingly difficult.
We'll start with the easier procedures and work our way up the difficulty scale.
and use the result. If you don't have this command handy, you can create one yourself using the
code near the bottom of this page, under Sample Perl Script: java_home.
If the answer is 1.7 or later, then this command will dump several dozen lines of property settings to
your screen:
% java -XshowSettings:properties -version
Scroll through the output to find the line (about a dozen lines from the beginning of the listing)
containing "java.home", and use the value part of that line.
Note that java dumps this listing to stderr rather than stdout, so the usual pipe through more will not
have the desired effect. You are going to have to either scroll or, on linux-based systems, redirect
error output to a file and then grep through that.
Setting JAVA_HOME
The JAVA_HOME environment variable can be set temporarily, so that when a script or batch file
finishes, or you close your command window, it goes away; or as part of your regular environment.
The syntax is roughly the same either way, but where you set the value changes.
On Windows Systems
To set the value temporarily from the command line, use the set command:
C:>set JAVA_HOME="C:\Program Files (x86)\Java\jre"
where spacing and capitalization count, and everything inside the quotes should be replaced with
the path you found via one of the methods in the previous section. The double quotes are required if
your path contains any imbedded blanks.
To create and set a JAVA_HOME variable as part of the default environment, use the method
appropriate to your particular flavor of Windows described here:
• How to set the path and environment variables in Windows, by ComputerHope.com
Then insert the path you found previously.
On Linux-based Systems
You will need to know what shell you are using and what the appropriate way to set environment
variables in that shell is. The two most popular shells are the Bourne shell and its relatives, and
different variations on the C-shell. We'll show syntax for both of those here. (Do "echo $SHELL" from
the command line to see what shell you are running if you don't know, and "man shell" on the shell
name to see what type it is.)
In Bourne-type shells, it takes two steps to set an environment variable that can be seen by any
programs you then run. For example:
% JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre
% export JAVA_HOME
You can include these lines in wrapper scripts, or you can set JAVA_HOME in your shell resource
file, so that it becomes an automatic part of your login environment.
Alternately, if you are on a Mac, or a friendly programmer has written you a routine that performs the
same function as the Mac java_home program, you can replace the explicit directory listed in the
code samples above with a call to this routine. For example, in the C-shell:
% setenv JAVA_HOME `java_home`
or, alternately:
% set JAVA_HOME=`java_home`