Question: I need on my Linux machine to compile Java programs. For this reason I have installed JDK (Java Development Kit), and now I'm trying to set the JAVA_HOME environment variable to point to the installed JDK. About setting JAVA_HOME environment variable on Linux, what way is the most respected?
Many Java programs or Java-based integrated development environment (IDE) will need to set the JAVA_HOME environment variable. This variable should point to the Java Development Kit (JDK) or Java Runtime Environment (JRE) installation directory. JDK includes not only all the JRE provided also with additional binaries and library files (such as compilers, debuggers and JavaDoc documentation generator) for compiling Java programs. JDK is used to build Java programs, if you just run a Java program has already been built, a JRE alone is sufficient.
When you are trying to set JAVA_HOME environment variable, JAVA_HOME variable troublesome thing that needs to change in accordance with the following: (1) Have you installed the JDK or JRE; (2) which version you have installed; (3) you have installed It is Oracle JDK or Open JDK.
So whenever your development environment or the runtime environment changes (for example, JDK update version), you need to adjust the JAVA_HOME variable according to the actual situation, and this approach is tedious and inefficient.
The following export command can automatically set the JAVA_HOME environment variable for you, without regard to the above factors.
If you are installing the JRE:
export JAVA_HOME = $ (dirname $ (dirname $ (readlink -f $ (which java))))
If you installed JDK:
export JAVA_HOME = $ (dirname $ (dirname $ (readlink -f $ (which javac))))
Depending on your situation, the above command will write a ~ / .bashrc (or / etc / profile) file, it will set the JAVA_HOME variable permanently.
Note that since java or javac can build multiple levels of symbolic links, for "readlink -f" command is used to get their real execution path.
For example, if you are installing Oracle JRE 7, then the above-described first export order is automatically set JAVA_HOME as follows:
/ Usr / lib / jvm / java-7-oracle / jre
If you are installing the Open JDK version 8, the second export command to set the JAVA_HOME:
/ Usr / lib / jvm / java-8-openjdk-amd64
In short, these export commands you to reinstall / upgrade your JDK / JRE, or replace the automatic update JAVA_HOME variable default Java version. You no longer need to manually adjust it.