📜  java jdk java_home home ubuntu openjdk-8-jdk - Java (1)

📅  最后修改于: 2023-12-03 15:31:31.143000             🧑  作者: Mango

Java JDK and Java Home on Ubuntu

Java is a widely used programming language that runs on almost every platform. In order to develop Java applications or run Java programs on your computer, you need to have the Java Development Kit (JDK) installed along with the Java Home environment variable set.

In this article, you will learn how to install Java JDK on Ubuntu and set Java Home environment variable to make Java work correctly on your system.

Installing Java JDK on Ubuntu

The easiest way to install Java JDK on Ubuntu is through the package manager. Open a terminal and execute the following commands:

sudo apt-get update
sudo apt-get install openjdk-8-jdk

This will download and install the OpenJDK 8 (Java Development Kit).

After the installation is complete, verify the Java version by running the following command:

java -version

You should receive output similar to the following, which tells you that Java is installed correctly:

openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
Setting Java Home environment variable

Java applications use the Java Home environment variable to locate the Java installation directory. By default, Ubuntu does not set this variable. You need to add it manually.

To set the Java Home environment variable, open a terminal and execute the following commands:

sudo nano /etc/environment

This will open the environment file in nano editor.

In the editor, add the following line at the end:

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

Note that the path mentioned in this line may be different on your system depending on your Java version and installation directory.

Press Ctrl+X, then Y, then Enter to save the file and exit the editor.

To activate the changes, you need to reload the environment file. Execute the following command to do so:

source /etc/environment

Now, if you run the echo $JAVA_HOME command, you should get the output:

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

This confirms that the Java Home environment variable has been set correctly.

Conclusion

In this article, you learned how to install Java JDK on Ubuntu and set the Java Home environment variable. With these steps, you should be able to successfully develop and run Java applications on your system.