This post will outline how to install Oracle Java JDK in Fedora, Red Hat or CentOS.
Please note that this will only install the JDK, when you’re done you will need to select this JDK to be able to use it. That is outlined in this blog post: Easily switch between java versions using alternatives in Linux
First determine if you need 32 or 64 bit. If you’re unsure you can check using “uname -p” in a terminal which will display your kernel and which architecture you’re using.
Next, download the JDK that you wish to use. In this example I will be installing the latest JDK from Oracle, you can find it here: Oracle Java Downloads.
I have chosen Java Platform (JDK) 8u31, which downloads the RPM.
After the download is finished, open a terminal and go to the location of the download.
Next, enter this command:
sudo yum install jdk-8u31-linux-x64.rpm
This will install the JDK.
Important: After this is done, you have placed all the files in the right location, but can’t use it until you’ve finished the steps in this blog post: Easily switch between java versions using alternatives in Linux
This approach works in several distros, I’ve been using it in Ubuntu for a while and just used it in Fedora as well.
As a developer, you might need to switch between java versions often, this approach will come in handy then.
We will be using the command “alternatives”, in this case to check the configuration of your Java installation. The default is most often OpenJDK, while you might need Oracle Java.
Run “alternatives –display java” to see which versions you can currently choose from:
[joris@today ~]$ alternatives –display java
java – status is manual.
link currently points to /usr/java/latest/bin/java
Current `best’ version is /usr/lib/jvm/java-1.8.0-openjdk-188.8.131.52-3.b13.fc21.x86_64/jre/bin/java.
There’s no Oracle Java yet, make sure you’ve installed Oracle Java. If you haven’t, you can check this blog post: Install Oracle Java in Fedora, Red Hat or CentOS using Yum and RPM
When Oracle Java is installed, you can add it to your alternatives: “sudo alternatives –install /usr/bin/java java /usr/java/latest/bin/java 20000”
Please note: I used “latest” in the command above, another options is to specifically set the version you want. This way you can install several JDK’s and switch as shown below.
When that is finished, you can select your current flavour of Java:
[joris@today ~]$ sudo alternatives –config java
There are 2 programs which provide ‘java’.
* 1 /usr/lib/jvm/java-1.8.0-openjdk-184.108.40.206-3.b13.fc21.x86_64/jre/bin/java
+ 2 /usr/java/latest/bin/java
Enter to keep the current selection[+], or type selection number:
Choose the option you want to switch between Java versions.
As mentioned by enkouyami, please check if you need to use update-java-alternatives instead of alternatives. The use of alternatives was valid when I wrote the post, but might not be working anymore!
Everybody knows that the java executable is located in /usr/bin/java , but what if you need the JDK / JRE location itself?
Just using “whereis” will not get you there, that will point you to the /usr/bin/java point.
So, let’s find out a but more about /usr/bin/java:
ls -l /usr/bin |grep java
Awesome, this will lead us somewhere, it’s a symlink to /etc/alternatives/java
So let’s do the same there:
ls -l /etc/alternatives/ |grep java
And we’ve hit the jackpot, among the lines here, there’s a bunch of lines pointing us to the JRE location:
As you can see in the screenshit, our java executable within the JRE location is: