How to install Java JDK on Raspberry Pi

raspberry pi
Java is one of the most popular programming languages ​​used to build various applications and systems. There are two different implementations of Java, Oracle Java and OpenJDK. Among them, OpenJDK is an open source implementation of the Java platform. Oracle Java has some other commercial features and is licensed for non-commercial use only. Here's how to install Java (OpenJDK) on Raspbian OS on Raspberry Pi.

Run the following command to install the latest JDK version, currently the OpenJDK 11 JDK:
sudo apt update
sudo apt install default-jdk
After the installation is complete, you can verify the Java version by checking the command:
java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Raspbian-1deb10u1)
OpenJDK Server VM (build 11.0.5+10-post-Raspbian-1deb10u1, mixed mode)
Install Java 8
Java 8 is still widely used today. If you need Java 8, the installation command is:
sudo apt update
sudo apt install openjdk-8-jdk
Check the Java version:
java -version
Multiple Java versions do not conflict. If you need to set the default version, you can use the following method. Run the java -version command to confirm the default version. If you need to modify the default version, you can use the update-alternatives tool:
sudo update-alternatives --config java
You will see the installed version of Java.
There are 2 choices for the alternative java (providing /usr/bin/java).
  Selection    Path                                            Priority   Status
* 0            /usr/lib/jvm/java-11-openjdk-armhf/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-armhf/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-armhf/jre/bin/java   1081      manual mode
Press  to keep the current choice[*], or type selection number: 
Enter the version number of the default version you want to set and press Enter.
If you have multiple JDK versions installed, to set the JAVA_HOME environment variable, you need to edit the /etc/environment file:
sudo nano /etc/environment
Assuming you want to set JAVA_HOME to OpenJDK 11, you can add at the end of the file:
The following path is what the update-alternatives command outputs.
Then run the command:
source /etc/environment
Finally, to uninstall the default-jdk package, just run:
sudo apt remove default-jdk