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

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.


I think the problem is that some Java packages rely on default-jre which points to openjdk-jre. I don't think there is a easy way to permanetely uninstall openjdk. The thing is that you probably will bump on openjdk again when you install a java package/program.

But if you're willing to have both, is it easy to set the java runtime programs (java, javah, javap, and so on) to sun implementation. Do it:

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

or if you are a java dev (install the sdk and srcs):

sudo apt-get install sun-java6-jdk sun-java6-jre sun-java6-source

And set it as the default programs with:

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