Skip to content

Instantly share code, notes, and snippets.

@LongClipeus
Last active March 25, 2023 19:00
Show Gist options
  • Save LongClipeus/77b0beda7a3a794688a5c4257007d3c8 to your computer and use it in GitHub Desktop.
Save LongClipeus/77b0beda7a3a794688a5c4257007d3c8 to your computer and use it in GitHub Desktop.
Install jdk from tar.gz file in Ubuntu
sudo su
#create jvm directory
mkdir /usr/lib/jvm
#uncompress, change to your file name
tar -zxf jdk-8u211-linux-x64.tar.gz -C /usr/lib/jvm
#check if files are there
#ls /usr/lib/jvm
#update alternatives so the command java point to the new jdk
update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-oracle/bin/java 100
#update alternatives so the command javac point to the new jdk
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-8-oracle/bin/javac 100
update-alternatives --display java
update-alternatives --display javac
#check if java is running
java -version

Set the Default Java Version

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.

Set the JAVA_HOME Environment Variable

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

Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment