To check the default Java version you would use the following command:
$ java -version
Output
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
If you have multiple Java versions installed on the server you can change the default version using the update-alternatives
tool as shown below:
$ sudo update-alternatives --config java
Output
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number:
To change the default Java version just enter the version number (the number in the Selection column) and press Enter.
Some applications written in Java are using the JAVA_HOME
environment variable to determine the Java installation location.
To set the JAVA_HOME
environment variable, use the update-alternatives
command to find where Java is installed:
$ sudo update-alternatives --config java
In our case the installation paths are as follows:
- OpenJDK 11 is located at
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
- OpenJDK 8 is located at
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Copy the installation path of your preferred installation. Next, open the /etc/environment
file:
$ sudo nano /etc/environment
Add the following line, at the end of the file:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Make sure you replace the path with the path to your preferred Java version.
You can either log out and log in or run the following source command to apply the changes to your current session:
$ source /etc/environment
To verify that the JAVA_HOME
environment variable is correctly set, run the following echo command:
$ echo $JAVA_HOME