
Run the following command to install the latest JDK version, currently the OpenJDK 11 JDK:
sudo apt update sudo apt install default-jdkAfter the installation is complete, you can verify the Java version by checking the command:
java -versionoutput:
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-jdkCheck the Java version:
java -versionMultiple 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 javaYou 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 PressEnter the version number of the default version you want to set and press Enter.to keep the current choice[*], or type selection number:
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/environmentAssuming you want to set JAVA_HOME to OpenJDK 11, you can add at the end of the file:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-armhf/bin/java"The following path is what the update-alternatives command outputs.
Then run the command:
source /etc/environmentFinally, to uninstall the default-jdk package, just run:
sudo apt remove default-jdk