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 11 on your Linux system.
This article is using JDK 11$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:
-
Install Oracle JDK 8 on Linux
- Install Oracle JDK 11 on Linux
- Install Oracle JDK 17 on Linux
-
Install Oracle JDK 18 on Linux
(The latest version)
-
Install the latest Oracle JDK on Linux
(Permanent link to install the latest version)
Step 1:
Download the latest
JDK(jdk-11$java_update_no_linux-x64_bin.tar.gz) from the official
site.
If you want to download to a remote server or if you simply prefer wget, use the command given in this StackOverflow answer: Downloading JDK
If you want to download to a remote server or if you simply prefer wget, use the command given in this StackOverflow answer: Downloading JDK
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-11$java_update_no_linux-x64_bin.tar.gz
According to this command, the JDK filename is
jdk-11$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/$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/$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/$java_version$java_update_no/bin"
JAVA_HOME="/usr/lib/jvm/$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/$java_version$java_update_no/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/$java_version$java_update_no/bin/javac" 0
sudo update-alternatives --set java /usr/lib/jvm/$java_version$java_update_no/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/$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.
25 comments
Write commentsdid not update /usr/lib/jvm/default-java/bin/java.
Replyupdate-alternatives --display java
java - manual mode
link best version is /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
link currently points to /usr/lib/jvm/jdk-9.0.1/bin/java
link java is /usr/bin/java
slave java.1.gz is /usr/share/man/man1/java.1.gz
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java - priority 1081
slave java.1.gz: /usr/lib/jvm/java-8-openjdk-amd64/jre/man/man1/java.1.gz
/usr/lib/jvm/jdk-9.0.1/bin/java - priority 0
The installation failed. It ended with this:
Replybash: /usr/bin/java: cannot execute binary file: Exec format error
Did you follow Step 7 and made sure the correct Java is selected in Step 8? If so, please check your operating system architecture. It should be 64 bit to install 64bit Java.
ReplyWorked perfectly for me. Thanks! :)
Replythank's it works....
ReplyHello,
ReplyI followed your instructions and worked perfect. Except I have a few questions. Before looking at this article I installed the openjdk version. I am saying this because when I look the path of other java tools (eg. jar, jarsigner) it traced back to the openjdk folder. I want to know, in the future, are there gonna be any problem with this? Of course in case I use these tools.
I don't know if I explained myself clear enough ':-).
Hi,
ReplyThere is nothing wrong with having OpenJDK. /usr/lib/jvm is used as the parent for all JDK installations. Suppose you also installed Oracle JDK 8, you will find it in the same folder. I don't find any issues with using tools from both Oracle JDK and Open JDK. You are fine with your current setup.
Thanks!!
ReplyI have jdk8 .0.212 installed in my Ubuntu PC, can I install another version of jdk like jdk11 this way?
ReplyYes you can but if you prefer to use JDK 8 as the default JDK, do not follow step 5-10.
ReplyHi, I've follow the tutorial until step 7, but, when i run the first command, this appears: update-alternatives: error: alternative path /usr/lib/jvm/jdk-11/bin/java doesn't exist
ReplyI don't know whats wrong.
Hi, Please share the output of the following command:
Replyls /usr/lib/jvm
Hey Laura,
ReplyIt looks like you are running the command exactly as the OP is.
He was using version 11, but if you are using the latest version, in my case it was 12.0.1, you will have to use the version that you have. So in my case, instead of using:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-11/bin/java" 0
I had to write:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-12.0.1/bin/java" 0
So figure out exactly what version you have (you can know if you run the command Gobinath mentioned) and replace 'jdk-11' with your version. (Ex: jdk-12.0.1)
(1) "update-java-alternatives --list" does not list this installation,
Reply(2) "update-java-alternatives --set" cannot switch to this installation's version,
I have to switch to 11.0.5 by the four commands shown in Step 7 (instead of one command). That is sub-optimal.
--------------
One of the reasons this installation disobeys "update-java-alternatives --set" is it does not have its symlink in usr/lib/jvm that my other jdk installations (versions) have there. It is via those symlinks "update-java-alternatives" handles (my other) jdk versions. I wonder how to make this installation to join the pack as well, so I could switch to "jdk-11.0.5" by that single command, please.
jar command not found how to fix that?
ReplyDid you add /usr/lib/jvm/jdk-11/bin to the PATH variable in Step 6?
Replyyeah i was missing the PATH variable silly mistake on my end.
Reply"The PATH variables must be separated by semi-colon." - Please remove or modify this line to "The PATH variable must be separated by colon". Semi-colon is for Windows systems and Colon for Linux.
ReplyGood catch. Thank you for pointing it out.
ReplyThanks for the instructions. Just a comment for all the newbies using linux terminal: after step 4, check the file name by running: ls /usr/lib/jvm. I have installed latest JDK 11 (jdk-11.0.6_linux-x64_bin.tar.gz) and the default folder is 'jdk-11.0.7'.
ReplyEnvironment variable updated to: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk-11.0.7/bin"
JAVA_HOME="/usr/lib/jvm/jdk-11.0.7"
Thanks for pointing it out. Updated the tutorial.
ReplyWhat if I need to use jdk11 with Android Studio but I need to run Thinkorswim with JRE 8?
ReplyHere is the the message from Thinkorswim:
"Oracle JRE 8 update 11 or later is required to run application.
Please manually update java and ensure it is the first Java available in your system path."
You mean JDK 11 for Android development right? Android Studio does not need JAVA_HOME or the java to be in bin path to be used as a development environment. You can choose whatever the Java installation from the project structure. I'd recommend setting Java 8 as the default Java (according to the Thinkorswim message) and keep JDK 11 extracted in the folder `/usr/lib/jvm` and add it to Android Studio manually.
ReplyI keep getting the following error: "bash: /usr/bin/java: cannot execute binary file: Exec format error" everytime i run the java -version command. Please advise
ReplyHi, Please check your system architecture. The JDK tar.gz file you've downloaded should match the system architecture. You can use the following command to check the system architecture
Replyuname -m
EmoticonEmoticon