Reputation: 9480
I installed java in my system around 6 months. I blindly followed this document's instruction here. I downloaded the jdk, unpacked it, set the paths Java_Home, Path according to the instructions in the doc.
Today I wanted to find out, which java is installed in my system and where. So, I looked it up online and ran this command
readlink -f $(which java)
It says
/usr/local/java/jre1.7.0_40/bin/java
But when I check Java_Home variable, it says
/usr/lib/jvm/jdk1.7.0_40
Here both paths refer to jdk 1.7, but they are different. One is in /usr/lib/jvm and other is in /user/local/java.
I think, due to some confusion, I set the java_home incorrectly. I must have unpakced jdk at couple of places. And, while setting up the java_home, I took incorrect path. Either that, or it is some linkage between two locations, which occurred due to some command I ran which I don't know of.
Anyway, I can run java programs correctly and run eclipse etc, so everything is fine and because of that I never noticed that.
But, I would like to know whether I need to fix the java_home variable to ensure that both of above commands return the same value. And, if it is not necessary, why is this set up working fine when 'readlink -f $(which java)' and java_home return different path.
Upvotes: 1
Views: 134
Reputation: 25494
Your JAVA_HOME should match your chosen version of Java. I set my JAVA_HOME with the following:
export JAVA_HOME=`readlink -f /usr/bin/javac | sed 's|/bin/javac||g'`
I use the Debain/Ubuntu update-java-alternatives
command to choose which version of Java I use. It makes sure that it doesn't just update /usr/bin/java
, but also all the other Java commands like javac
and javadoc
.
sudo update-java-alternatives --set java-7-oracle
You can get a list of the Java distributions installed on your system:
update-java-alternatives -l
Most programs don't rely on both JAVA_HOME and which java
is in your path. Typically they rely on one or the other, but not both. So for any given application, it will probably run, but different applications may run using different versions of Java. In my experience, most installed java applications will use /usr/bin/java
and most development environments will use JAVA_HOME.
Specifying both JAVA_HOME and setting /usr/bin/java
correctly ensure that almost all Java application run with your preferred (and the latest) version of Java.
There are some notable exceptions to that rule, however. Some programs that use run under Java have their own configuration to choose which version of Java they are using.
For example I run the tomcat7 server, and I have to have the configuration file /usr/share/tomcat7/bin/setenv.sh
that sets the JAVA_HOME directory for just that application:
JAVA_HOME=`readlink -f /usr/bin/javac | sed 's|/bin/javac||g'`
JAVA_OPTS="-Xms256m -Xmx2048m -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:MaxPermSize=512M -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
Other programs like Eclipse completely ignore JAVA_HOME. It has its own algorithm for figuring out which version of Java it will run under and has a -vm
command line option that allows you to specify your preference.
Upvotes: 2
Reputation: 40048
readlink -f $(which java)
just tells you which file is executed when you type java
in your shell. JAVA_HOME
is an environment variable used by some programs to locate java. Since both variables point to a 1.7
jre/jdk, it should not matter much that they differ. Programs using JAVA_HOME
will use the JDK java
and programs just using java
command line will use the JRE version.
If you really need to change them to point to the same java, then you should NOT change your JAVA_HOME
but instead your java
symlink, because pointing to a JDK is usually better than pointing to a JRE, since some programs mandate that JAVA_HOME
must point to a JDK (for example, because those programs need javac
which is not included in the JRE).
Upvotes: 2
Reputation: 3749
It's two different things.
The first tells you where the JRE (Java Runtime Environment), the java binary is located. You need this to run JAR files.
JAVA_HOME tells you where the JDK (Java Development Kit), the javac binary is located. You need this to compile and package JAR files.
Upvotes: 1