Ubuntu – How to correctly remove OpenJDK/OpenJRE and set SunJDK/SunJRE as Default

java

Ubuntu seems to favour OpenJDK/JRE very much over Sun JDK/JRE. Even after I installed Sun JRE, JDK and plugin and spent some time plucking out OpenJDK-related packages, apt-get has installed them back with some packages as a dependency. Can this behaviour be corrected in favour of Sun Java packages? I'd like to have one and only Java stack installed (yes, it's a bit of OCD, but I like to have my systems clean) and want it to be Sun Java.

Update: as Marcos Roriz notes, the problem seems to be in default-jre (on which Java-dependent packages use to depend) pointing to OpenJDK, so the question seems to go about how to hack default-jre/default-jdk to point to Sun Java.

Best Answer

Installing Sun Java on Older Versions of Ubuntu (9.10 and below)

I had the same problem once about three years ago with Ubuntu 8.04 I think. There was a weird problem when using openjre and the problem was solved by using the sun jre version. Here are the steps in the terminal which apply to versions below 10.04, since starting from 10.04 sun java has been dropped and replaced by openjre:

sudo apt-get remove openjdk-6-jre default-jre default-jre-headless

sudo apt-get install sun-java6-jre sun-java6-bin sun-java6-plugin sun-java6-fonts

(Everything Sun java related. Just to make sure)

After that, if you removed openjdk then you would only have sun java. If you did not remove it then you have both of them. To change from one to another do this:

sudo update-java-alternatives -l - This will show you the list of java engines you can use.

The name of the java alternative is the one in the left so for example openjdk would appear like this:

java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk

And you would set the system to openjdk like this:

sudo update-java-alternatives -s java-6-openjdk

If you would want to use Sun then you would do this (After installation of the sun packages):

sudo update-java-alternatives -s java-6-sun

If you want to use the java 7 version, simply change the number 6 mentioned above for 7. For example if I wanted to install the openjre version 7 I would:

sudo apt-get install openjdk-7-jre default-jre default-jre-headless

Installing Sun Java on Newer versions of Ubuntu (10.04 an above)

Open the terminal and type the following:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update   
sudo apt-get upgrade   
sudo apt-get install oracle-java7-installer

That should install the Sun Java version on your system. To change to it simply do the following in case you have other java alternatives:

sudo update-java-alternatives -s java-7-oracle  

If you want the installation to be automatic type the following:

sudo echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections

after that simply do the following to set the default environment variables:

sudo apt-get install oracle-java7-set-default

Note that the PPA includes versions 6, 7 and 8. So you can install any of them by simply changing the number in the installation line, for example:

sudo apt-get install oracle-java8-installer

Would install Java 8. After installing the Java version you wish to use and setting the version to the default with the update-java-alternatives command, you can check which version you are using by typing the following:

java -version

It will show you the default version you are using at that moment.

I would like to also mention that OpenJRE/JDK has come a long way from 2 or 3 years ago when it had many issues that were solved by changing to Sun Java.