Finding and Setting JAVA HOME

From The SBN Wiki
Revision as of 17:55, 24 March 2015 by Raugh (talk | contribs) (Creation - Safety Save)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.

Is JAVA_HOME Already Set?

Sometimes your JAVA_HOME environment variable will already be set for other reasons. To check, on the Windows command line do:

  C:> echo %JAVA_HOME%

or on linux-based systems, do:

  % echo $JAVA_HOME

If the answer is a directory path, you're good to go.

Finding the Java Home Directory

The JAVA_HOME environment variable needs to point to the installation directory on your system that contains the bin/ directory where the java executable physically resides, and the lib/ directory which contains the core java libraries and properties files. For linux users, it is almost certainly not the same directory that you get if you do "which java" from the command line, because that would be too easy. So if JAVA_HOME isn't already set, your next task is to find the directory to which you need to set it.

Are You Using a Mac?

If so, then the Apple system programmers have done you a favor and provided a handy little command called "java_home" that returns the value you need. Just do this from the command line:

   % java_home

and use the result.

Is Your Java Version 1.7 or Later?

If your Java version is fairly recent, there's a useful command option that lists out a whole bunch of system property settings, including the value of java.home, which is what we need. To see your Java version (Windows or Linux), do:

   % java -version

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're going to have to either scroll or, on linux-based systems, redirect error output to a file and then grep through that.

Are You, or Do You Know, a Friendly Java Programmer?

The value needed is the value of the java.home property, which is available from inside a java executable if you know what you're doing. A java programmer should be able to write a reasonably short program to print this value for you on demand, so you never have to bother him or her again. I'm not a java programmer, so if you happen to have a program like this you'd like to donate to the cause, please get in touch.

Alternately, if you have the privileges you can update your java installation to the most recent version and use the "Java Version 1.7 or later" method, above.

Are You Ready to Do It the Hard Way?

As a last resort, you'll have to dig through system directories, trying to find the java installation files. This can be tricky, since the installation might be for the Java Developers Kit (look for directories starting with "jdk"), or the Java Runtime Environment (look for directories starting with "jre"), or it might be referenced as the Java Virtual Machine (look for directories starting with "jvm").

Search functions are your friend, here. On Windows machines, first look for a "Java/ directory in your "Program Files", "Program Files (x86)" or equivalent system directory. If you find one, look down into it until you find the subdirectory containing the bin/ and lib/ directories. If that fails, try searching for the java executable itself, and use the name of the directory that contains the bin/ directory that, in turn, contains the java executable.

On linux-based systems, where there is much greater scope for variability, try running the find command on directories like /usr/lib or /usr/share. (You should have permission to read any directories that might contain java libraries you'd reference, so you can ignore "Permission denied" messages.)

If all else fails, try buying your system administrator a decent cup of coffee and asking nicely.