Uninstall Java from Ubuntu Linux

Introduction

Hopefully you won’t need these instructions due to a botched install, but there may come a time where you need to uninstall a version of the JDK/JVM.

These instructions are for the Oracle JDK 1.7.0 Update 4 on Ubuntu 12.04 LTS. If you are using a different version of the JDK, then change the version numbers listed below.

I have also included instructions for removing the OpenJDK at the bottom of this post.

Uninstall Java

Let’s check the current setup before we uninstall Java.

sudo update-alternatives --display java

The output from the command will be something like:

java - manual mode
link currently points to /usr/lib/jdk1.7.0_04/bin/java
/usr/lib/jdk1.7.0_04/bin/java - priority 1
/usr/lib/jvm/j2sdk1.6-oracle/jre/bin/java - priority 315
slave java.1.gz: /usr/lib/jvm/j2sdk1.6-oracle/man/man1/java.1.gz
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java - priority 1051
slave java.1.gz: /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java'.

Next, we’ll remove each symlink to a Java binary from the Debian alternatives system. I have split the following commands onto multiple lines to ensure that they display correctly on this page. However, you can remove the \ and then type each command on one line in the terminal.

sudo update-alternatives --remove "java" \
"/usr/lib/jvm/jdk1.7.0_04/bin/java"
sudo update-alternatives --remove "javac" \
"/usr/lib/jvm/jdk1.7.0_04/bin/javac"
sudo update-alternatives --remove "javaws" \
"/usr/lib/jvm/jdk1.7.0_04/bin/javaws"

Let’s quickly verify that the commands above remove the symlinks.

java -version
javac -version
which javaws

You should no longer see 1.7.0 u04 for the version of any of the above commands.

IMPORTANT WARNING
You must type the next 2 commands perfectly to avoid permanently destroying your system. If you do this wrong, you could delete important system files, including those that are required by Ubuntu.

cd /usr/lib/jvm
sudo rm -rf jdk1.7.0_04
sudo update-alternatives --config java

Output:
update-alternatives: error: no alternatives for java.

sudo update-alternatives --config javac

Output:
update-alternatives: error: no alternatives for javac.

sudo update-alternatives --config javaws

Output:
update-alternatives: error: no alternatives for javaws.

sudo vi  /etc/environment

Delete the line with JAVA_HOME

Uninstall OpenJDK (if installed)

First we’ll check which OpenJDK packages are installed.

sudo dpkg --list | grep -i jdk

Next, let’s uninstall OpenJDK related packages. Edit the list of packages below if you have additional or different OpenJDK packages installed.

sudo apt-get purge icedtea-* openjdk-*

Let’s check that all OpenJDK packages have been removed.

sudo dpkg --list | grep -i jdk
About these ads

About akbarsahmed

Technology executive with a passion for efficiency.

Posted on June 24, 2012, in Software Development and tagged . Bookmark the permalink. 9 Comments.

  1. Thank you; so simple!

  2. Akbar – Wow, fantastic explanation….very useful and clear step by step instructions. Keep up the good work and thanks for your effort…..!!!!

  3. increase pinterest followers

    I am really fascinated by your article here, i have a facebook group of pretty much same category, i was wodering if i can post your stuff there in my facebook group by your permission, ofcourse.

  4. It’s a nice Sequence of steps with good Explanation.
    But still I am Getting java version when i typed “which java”.
    what would be the problem?

  5. Thank you….!

  6. I could not run my own installed java version. I have unintalled the java from the system using above commands. Downloaded the jdk from following site and uncompressed to my home directory.
    http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

    Now added the path to “PATH” variable in the /etc/environment file like below:
    PATH=”:/home/user/jdk1.7.0_40/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games”

    Added following like to ~/.bashrc file
    export JAVA_HOME=/home/niaz/jdk1.7.0_40/bin/java

    If I run from console the command >java -version
    it outputs like following:

    The program ‘java’ can be found in the following packages:
    * default-jre
    * gcj-4.6-jre-headless
    * openjdk-6-jre-headless
    * gcj-4.5-jre-headless
    * openjdk-7-jre-headless
    Try: sudo apt-get install

    But if I go to my home directory, where java is uncompressed and execute like >./java -version. It outputs the version correctly.

    Could you please tell what is missing to have my own java version on the central point for all programs. Thanks.

  1. Pingback: Java Exception in thread “main” java.lang.UnsupportedClassVersionError | 3StrandsOfHair

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: