Home  >  Article  >  Computer Tutorials  >  How to set JAVA_HOME in Linux

How to set JAVA_HOME in Linux

WBOY
WBOYforward
2024-03-20 17:41:20595browse

How to set JAVA_HOME in Linux

Setting JAVA_HOME is essential when you want to develop Java-based applications on a Linux system. JAVA_HOME points to the directory where the JDK or Java Development Kit is installed.

Thus, the JAVA_HOME variable plays a vital role in Java applications as it helps determine the location of libraries and binaries that need to function correctly. Additionally, the JAVA_HOME variable provides the JDK with access to all programs that require Java. Although it can be configured immediately after installing the JDK or Java Runtime Environment (JRE), most beginners may not know much about the correct way to set it up. Therefore, here is a brief introduction to the simplest way to easily set JAVA_HOME in a Linux system.

How to set JAVA_HOME in Linux

First, you need to find the location of Java installation in the system and copy its path. This step is crucial because later on, you will set this path to the value of the JAVA_HOME variable.

Normally, Java is installed in the "/usr/lib/java" directory, but when operating on a Linux virtual machine, the installation directory may be "/usr/lib/jvm". You can view the installed Java version by going to this directory and executing the "ls" command.

As shown in the picture above, our installation directory is as follows:

/usr/lib/jvm/java—11—openjdk—amd64

To open your shell configuration file, you need to use a text editor. Configuration file paths vary from shell to shell, make sure you look for the correct path.

  1. For Bash: ~/.bashrc
  2. For Zsh: ~/.zshrc

For example, let us use the following command:

nano~/bashrc

The file will open in a new window. Go to the end of the file and append the following text:

export JAVA_HOME =/usr/lib/jvm/java—11—openjdk—amd64

Replace "/usr/lib/jvm/java—11—openjdk—amd64" with the path copied in the previous step.

When finished, exit the editor. Now you can restart the terminal or enter the command like this:

source~/.bashrc

Now you have successfully set JAVA_HOME. To make sure it, you can verify it using the following command:

echo $JAVA_HOME

in conclusion

The JAVA_HOME environment variable is essential for every application running on Java. However, users need to know its configuration process, so we explain how to set JAVA_HOME in Linux. The overall approach is simple: you have to first copy the Java installation path and then export it as the value of the JAVA_HOME variable in the shell configuration file.

The above is the detailed content of How to set JAVA_HOME in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete