Some distributions, such as Linux Mint, have OpenJDK installed right out of the box. For Ubuntu Linux, however, if we are to run applications programmed in Java, we need to install Java. In this guide, we will install Java on Ubuntu using both OpenJDK, and the official OracleJDK.
Unblock any international website, browse anonymously, and download movies and Mp3 with complete safety with CyberGhost, just for $2.75 per month:
We have prepared this guide on Ubuntu Linux 15.04 and Linux Mint 17.2. It should work to install Java in recent earlier and future versions. It might also work for Linux distributions based on Ubuntu/Debian.
What's the difference between OpenJDK and OracleJDK?
There are two options to install Java on Linux: The open-source OpenJDK and the official OracleJDK.
Is there a difference between the two? Which one should we pick, and why?
The fact is, there are no huge differences between OracleJDK and OpenJDK, they are very close.
As answered on the Official Oracle Blog, the build process of OracleJDK builds on OpenJDK by adding just a couple of pieces, which include the deployment code and some closed source third-party components. It works similarly to Chromium and Google Chrome.
For most Java applications, OpenJDK is good enough. After all, it is maintained by Oracle engineers, so it is in no way an "inferior" type of Java. If we don't need the third-party components of OracleJDK, like JRockit Mission Control, OpenJDK is perfectly adequate.
If OpenJDK doesn't work with a Java application, we should install OracleJDK.
Do we already have Java installed?
If we are using a distribution based on Ubuntu, it might already have OpenJDK installed, like Linux Mint does.
To check that, we just need to open a terminal with Ctrl+Alt+T and type:
java -version
If Java is already installed, we will get the current version.
If it isn't, on some distributions we will get a list of packages that include Java.
How to install Java in Ubuntu with openJDK
To install Java with OpenJDK on Ubuntu, we open a terminal and type:
sudo apt-get install openjdk-8-jre -y
This command will install the Java Runtime Environment (JRE) and should be enough for most users.
If you are new to Linux, remember that when we are asked to enter our Password on the terminal, for the sudo command, nothing will appear as we are typing, no stars or dots or anything. We just type our password and press Enter.
The current version of Java, as of this writing, is version 8, that is why we are are using openjdk-8-jre.
When Java reaches version 9, which is expected on September 22, 2016, the same command will work with openjdk-9-jre.
After we install Java, the "java -version" command will return the proper version.
Install Java to compile
To compile Java applications, we need to install the Java Development Kit (JDK).
This is essential if we are writing code in Java or want to use certain applications such as Apache Ant, Apache Maven, Eclipse, IntelliJ IDEA, etc.
The command for the Java Development Kit is:
sudo apt-get install openjdk-8-jdk -y
After September 22, 2016, we can replace the command with openjdk-9-jdk to get the Java 9 Development Kit.
How to install Java in Linux Mint or Ubuntu with OracleJDK
To install Java using the OracleJDK, we first need to install Python Software Properties, with:
sudo apt-get install python-software-properties -y
Then, we add the special java repository that is maintained by the Webupd8 team.
sudo add-apt-repository ppa:webupd8team/java -y
Any time we add a repository, we then have to do an update, so the new packages are available for installation.
sudo apt-get update
Finally, we install Java with:
sudo apt-get install oracle-java8-installer -y
The installation will ask us to read the Java license at http://java.com/license.
We must agree with the Oracle Binary Code license terms, or the installation won't proceed.
The rest of the installation is automatic. The installer will download 173MB...
...and complete the installation.
How to set the "JAVA_HOME" environment variable
Some applications need to know the path where Java is installed, using the JAVA_HOME environment variable, which is not automatically created when we install Java.
To create this variable, we will first need to find the path of installation with the command:
sudo update-alternatives --config java
If we installed the OpenJDK, it will probably be
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
For the OracleJDK it will probably be
/usr/lib/jvm/java-8-oracle/jre/bin/java
We can copy the path by selecting it with the mouse and right-click -> copy, or pressing Ctrl+Shift+C (the plain Ctrl+C won't work on the Linux Terminal).
Then, we open the file /etc/environment with gedit:
sudo gedit /etc/environment
Don't worry about the IBUS-WARNING, it shows every time we run gedit through the terminal as root.
At the end of the file, we add the following line, containing our Java installation path we just copied:
JAVA_HOME="[our path]"
We save the file and close gedit. Then, we reload the environment file, with:
source /etc/environment
To test if everything went well, we run:
echo $JAVA_HOME
This should show our Java installation path.
Remember, the Linux terminal is case sensitive, java_home or Java_Home won't work.
Did you have any trouble to install Java on Linux Mint or Ubuntu?
If anything on this guide didn't work for you as described, and you couldn't install Java on Linux Mint or Ubuntu, leave us a comment.
Support PCsteps
Do you want to support PCsteps, so we can post high quality articles throughout the week?
You can like our Facebook page, share this post with your friends, and select our affiliate links for your purchases on Amazon.com or Newegg.
If you prefer your purchases from China, we are affiliated with the largest international e-shops:
gentlemen, i have a linux mint which has java openjdk by default. i have followed the instruction above, except installing the openjdk because it was already installed by default. everything was ok, but still i don't have "javac". i mean "javac" is not working. i am using ZSH terminal. while i am using the follow instructions: "which java; whereis java; java -version......" and others, all are working and giving the correct result, but only "javac" is not working. kindly anyone could advice about that? thank you
Hi Khaidoun
Try this solution: http://askubuntu.com/questions/117189/apt-get-install-openjdk-7-jdk-doesnt-install-javac-why
hello angelos, thank you very much for answering my issue, and sorry for taking so long to try it and reply. but unfortunately the solution did not work for me. i have opened the path "/usr/lib/jvm/java-7-openjdk-amd64/bin" but i did not find "javac". so the "
sudo update-alternatives --config javac" it is not working for me. and i tried the "sudo update-alternatives --config java", the result was "there is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java; nothing to configure. even in the jre/bin there is no javac. any advice about that
100% working..thanks a lot