Even though OpenJDK is available in Linux repositories, some
applications strictly require Oracle Java Development Kit. This article
shows you how to manually install Oracle JDK $java_version on your Linux
system. This article uses JDK $java_version$java_update_no to
demonstrate the installation. In the provided commands, replace the
version specific paths and file names according to your downloaded
version.
Version specific installation guides are available here:
Version specific installation guides are available here:
-
Install Oracle JDK 8 on Linux
- Install Oracle JDK 11 on Linux
- Install Oracle JDK 16 on Linux
-
Install Oracle JDK 17 on Linux
(The latest version)
-
Install the latest Oracle JDK on Linux
(Permanent link to install the latest version)
If your Linux distribution is using DEB package format like Debian, you can download and install the jdk-$java_version$java_update_no_linux-x64_bin.deb file using the following command:
sudo dpkg -i jdk-$java_version$java_update_no_linux-x64_bin.deb
If your Linux distribution is using RPM package format like Cent
OS, you can download and install the
jdk-$java_version$java_update_no_linux-x64_bin.rpm file using the
following command:sudo rpm -ivh jdk-$java_version$java_update_no_linux-x64_bin.rpm
However, this article explains the manual installation method which is applicable for all Linux distributions out there. Personally, I prefer the manual installation because I have more control over the changes made in the system.
Step 1:
Download the latest
JDK(jdk-$java_version$java_update_no_linux-x64_bin.tar.gz) from
this official site:
If you want to download to a remote server or if you simply prefer wget, use the following command:
If you want to download to a remote server or if you simply prefer wget, use the following command:
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/16.0.2+7/d4a915d82b4c4fbb9bde534da945d746/jdk-16.0.2_linux-x64_bin.tar.gz
Step 2:
Open the terminal (Ctrl + Alt + T) and
enter the following command.
sudo mkdir /usr/lib/jvm
If the /usr/lib/jvm folder does not exist, this command will
create the directory. If you already have this folder, you can ignore
this step and move to the next step.
Step 3:
Enter the following command to change the directory.
cd /usr/lib/jvm
Step 4:
Extract the jdk-Xuxx-linux-xXX.tar.gz file in that directory
using this command.
sudo tar -xvzf ~/Downloads/jdk-$java_version$java_update_no_linux-x64_bin.tar.gz
According to this command, the JDK filename is
jdk-$java_version$java_update_no_linux-x64_bin.tar.gz and which
is located in the ~/Downloads folder. If your downloaded file is
in any other location, change the command according to your path.Step 5:
Enter the following command to open the environment variables file.
sudo nano /etc/environment
According to your personal preference, you can choose any text editors instead of nano.
Step 6:
In the opened file, add the following bin folder to the existing PATH
variable.
/usr/lib/jvm/jdk-$java_version$java_update_no/bin
The PATH variables must be separated by colon.
Add the following environment variables at the end of the file.
JAVA_HOME="/usr/lib/jvm/jdk-$java_version$java_update_no"
The environment file before the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
The environment file after the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk-$java_version$java_update_no/bin"
JAVA_HOME="/usr/lib/jvm/jdk-$java_version$java_update_no"
Save the changes and close
nano
(Ctrl + O, Ctrl + X).
To learn more about setting environment variables and/or to set the environment variable without root privilege, check How to Set Environment Variables in Linux?.
Step 7:
Enter the following commands to inform the system about the Java's
location. Depending on your JDK version, the paths can be different.
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-$java_version$java_update_no/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-$java_version$java_update_no/bin/javac" 0
sudo update-alternatives --set java /usr/lib/jvm/jdk-$java_version$java_update_no/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/jdk-$java_version$java_update_no/bin/javac
Step 8:
To verify the setup enter the following commands and make sure that they
print the location of java and javac as you have provided in the
previous step.
update-alternatives --list java
update-alternatives --list javac
Step 9:
Restart the computer (or just log-out and login) and open the terminal
again.
Step 10:
Step 10:
Enter the following command.
java -version
If you get the installed Java version as the output, you have
successfully installed the Oracle JDK in your system.
8 comments
Write commentsThank You Very Very Much Sir... You solved my biggest problem till now..
ReplyGlad to hear that you find it useful.
ReplyThank you*100
ReplyI scoured all the websites and found my answer finally!
My god. This ended my failed endeavor looking for this. And, Im happy I found your site! Will be using it more often.
ReplyGlad you find it useful
ReplyThanks. Glad you find it useful.
ReplyI get an error when I try to run the java -version:
Replybash: /usr/bin/java: cannot execute binary file: Exec format error
any ideas? I double-checked my work to make sure it was accurate.
Hi, Sorry for the late reply. Did you download the JDK for your system's architecture. If you are running x86_64 system, make sure you've downloaded the x64 file. If you are running an ARM machine, you need to download the ARM version.
ReplyEmoticonEmoticon