Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to set JAVA_HOME environment variable in Linux

Shulou Source: shulou.com Published: 2022-06-01 17:04:01 10月04日 Update

It is believed that many inexperienced people have no idea about how to set JAVA_HOME environment variable in Linux. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Problem: I need to compile the Java program on my Linux machine. I've installed JDK (Java Development Kit) for this, and now I'm trying to set the JAVA_HOME environment variable to point to the installed JDK. What is the most popular way to set the JAVA_HOME environment variable on Linux?

Many Java programs or Java-based integrated development environments (IDE) need to set the JAVA_HOME environment variable. This variable should point to the installation directory of the Java Development Kit (JDK) or Java Runtime Environment (JRE). JDK not only contains everything that JRE provides, but also comes with additional binaries and library files (such as compilers, debuggers, and JavaDoc document generators) for compiling Java programs. JDK is used to build Java programs, and a single JRE is sufficient if you just run a Java program that has already been built.

When you are trying to set the JAVA_HOME environment variable, the trouble is that the JAVA_HOME variable needs to change based on (1) whether you have installed JDK or JRE;, (2) which version you installed, and (3) whether you installed Oracle JDK or Open JDK.

So whenever your development or runtime environment changes (for example, an updated version of JDK), you need to adjust the JAVA_HOME variable according to the actual situation, which is onerous and inefficient.

The following export command automatically sets the JAVA_HOME environment variable for you, regardless of the above factors.

If you are installing JRE:

Export JAVA_HOME=$ (dirname $(dirname $(readlink-f $(which java)

If you are installing JDK:

Export JAVA_HOME=$ (dirname $(dirname $(readlink-f $(which javac)

In your case, write one of the above commands to the ~ / .bashrc (or / etc/profile) file, and it will * set the JAVA_HOME variable.

Note that because java or javac can establish multiple levels of symbolic links, the "readlink-f" command is used to get their actual execution path.

For example, if you install Oracle JRE 7, the above export command will automatically set the JAVA_HOME to:

/ usr/lib/jvm/java-7-oracle/jre

If you install Open JDK version 8, the second export command sets the JAVA_HOME to:

/ usr/lib/jvm/java-8-openjdk-amd64

After reading the above, have you mastered how to set the JAVA_HOME environment variable in Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Variables environment commands programs for this problem development run content situation point method more version compile onerous helpless editor thing binary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Shulou Tech Info Docker MySQL