204

How can I completely remove all traces of Java on my system?

I already know how to install it in case I need it again.

CaldwellYSR
  • 2,207

9 Answers9

422
  1. Remove all the Java related packages (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ):

    dpkg-query -W -f='${binary:Package}\n' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e '^java-common' | xargs sudo apt-get -y remove
    sudo apt-get -y autoremove
    
  2. Purge config files (careful. This command removed libsgutils2-2 and virtualbox config files too):

    dpkg -l | grep ^rc | awk '{print($2)}' | xargs sudo apt-get -y purge
    
  3. Remove Java config and cache directory:

    sudo bash -c 'ls -d /home/*/.java' | xargs sudo rm -rf
    
  4. Remove manually installed JVMs:

    sudo rm -rf /usr/lib/jvm/*
    
  5. Remove Java entries, if there is still any, from the alternatives:

    for g in ControlPanel java java_vm javaws jcontrol jexec keytool mozilla-javaplugin.so orbd pack200 policytool rmid rmiregistry servertool tnameserv unpack200 appletviewer apt extcheck HtmlConverter idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc xulrunner-1.9-javaplugin.so; do sudo update-alternatives --remove-all $g; done
    
  6. Search for possible remaining Java directories:

    sudo updatedb
    sudo locate -b '\pack200'
    

    If the command above produces any output like /path/to/jre1.6.0_34/bin/pack200 remove the directory that is parent of bin, like this: sudo rm -rf /path/to/jre1.6.0_34.

Nav
  • 1,059
Eric Carvalho
  • 54,385
  • 7
    Have you tested this answer? I'm reasonably certain you've got it all, but just double checking. :) – Jorge Castro Sep 10 '12 at 19:03
  • 17
    @JorgeCastro, absolutely. I built those commands one by one, then piped it all together and I ran all of them in different Java installation scenarios that I have for test purposes in some workstations at work. I really paid attention to the output of grepthat is piped to apt-get remove, I think that it covers almost all possibilities regarding installation by deb packages. – Eric Carvalho Sep 11 '12 at 00:50
  • 5
    Although the presented sequence of commands works on most cases, I think my answer needs some improvements. Everybody, please, feel free to suggest changes that can improve it, like better explanation of each command or pointing out a scenario not covered by them. – Eric Carvalho Sep 11 '12 at 01:11
  • 4
    +1 for the answer. I recently found a scenario where removing openjdk causes installation of other java packages. A workaround for this problem is disabling all of the repository and then execute the removal command. – Anwar Sep 11 '12 at 08:54
  • 1
    @EricCarvalho When I issued the command sudo bash -c 'ls -d /home/*/.java' | xargs sudo rm -rf the response from the terminal was stated as invalid. – n00b Mar 19 '14 at 15:48
  • 3
    @n00b probably because there's no .java directory in your home. What was the actual error message? – Eric Carvalho Mar 19 '14 at 16:34
  • 1
    @EricCarvalho The actual error message is: ls: cannot access /home/*/.java: No such file or directory – n00b Mar 19 '14 at 22:42
  • @EricCarvalho Could you also answer my other questions that I posted? If you scroll down this page, you will see my post beginning with the words I followed strictly the steps that Eric Carvalho provided to Thanks. – n00b Mar 19 '14 at 22:45
  • @n00b Nothing to worry about. That command deletes the java configuration directory (.java) if it exists. If it doesn't exist, you'll get an error, that's OK. – Eric Carvalho Mar 19 '14 at 23:30
  • @EricCarvalho: Am I correct to say that the command beginning with apt-cache search java | awk '{print($1)}' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e.... is for the 32-bit OS. I have installed a 64-bit OS. What is the corresponding command? Thanks. – n00b Mar 22 '14 at 16:43
  • @EricCarvalho: The command sudo apt-get -y autoremove removes many other packages that I need. Is it OK to just ignore this command? Or could you provide a "gentler" command that does not remove packages other than those have Java in them? – n00b Mar 22 '14 at 16:46
  • This was great! Thanks a lot. I had difficult time in setting Oracle JDK 1.7 as my default in presence of OpenJDK which I wasn't needing. So removed all and installed that. – Ravindra S Jul 03 '14 at 14:42
  • Magic! This worked for me on Crunchbang 3.2.0-4-amd64. – pdoherty926 Sep 13 '14 at 16:24
  • 1
    Hm. Not sure if this was the cause of the error but suddenly getting http://askubuntu.com/questions/141606/how-to-fix-the-system-is-running-in-low-graphics-mode-error on Lenovo T440 w ubuntu 14 when I ran this – bernie2436 Dec 28 '14 at 20:48
  • 1
    `$ sudo locate -b '\pack200'

    /usr/share/bash-completion/completions/pack200`

    I should not remove this. Right?

    – Atiq Rahman Apr 26 '15 at 06:58
  • @AlexeyBobrick That's because JabRef depends on a Java. Removing a package will remove all the packages that depend on it. – Eric Carvalho Jul 02 '15 at 11:30
  • @EricCarvalho: Aha, okay! I first had a guess that it was just because jabref as a string matched the pattern. – Alexey Bobrick Jul 03 '15 at 09:18
  • 1
    apt-cache search java ... | xargs sudo apt-get -y remove This unfortunately removes libreoffice, libreoffice-java-common, libreoffice-sdbc-hsqldb on my machine (Mint x64, running Libreoffice 5.0.1). – direwolf Sep 05 '15 at 07:48
  • 1
    Isn't it also a good idea to get rid of environment variables such as JAVA_HOME? These would typically be located in either /etc/environment, /etc/profile, /etc/profile.d/* or even ~/.bashrc, ~/.profile. It depends on how you've installed it. Mine was in /etc/profile.d/jdk.sh. – Housni Mar 01 '16 at 11:57
  • 1
    Is this right? These things seem unrelated to me: $ dpkg -l | grep ^rc | awk '{print($2)}'
    apt-show-versions
    liblcms1:armhf
    libpython2.7
    libwnck22
    libxres1:armhf
    python-support
    stunnel4```
    
    – msouth Feb 16 '17 at 18:28
  • 2
    You SHOULD probably mention it will delated a lot of unrelated things... I got my Netbeans removed and a lot of other apps, ffs... – pzaj Jun 20 '17 at 13:21
  • I had to modify all the xargs command a bit to get it to work. After xargs I added -I{} and at the end of the command I added {} So, for example instead of sudo bash -c 'ls -d /home/*/.java' | xargs sudo rm -rf I run sudo bash -c 'ls -d /home/*/.java' | xargs -I{} sudo rm -rf {} – mgershen Nov 07 '18 at 15:58
  • Is autoremove is a good thing to try out, So far I know it removes lot other libraries which may eventually break the Ubuntu system – coder3521 Jun 25 '19 at 08:49
  • my biggest problem turned out to be "IBM J9 VM". The un-install was difficult to identify but easy to perform: sudo apt-get purge ibm-java...... (here it was sudo apt-get purge ibm-java80-jre). – opinion_no9 Sep 18 '21 at 19:28
  • use find /*/*java* to find anything else remaining. – Rajan Dec 06 '21 at 18:34
  • 1
    @Rajan and then delete all the files listed on its output via sudo find /*/*java* | xargs rm -f – Tms91 Feb 02 '23 at 00:35
53

To completely remove OpenJDK on Ubuntu 11.10 (this may or may not be sufficient on other versions of Ubuntu), run:

sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*

If you want instructions for removing the proprietary Oracle ("Sun") version of Java, then you'll have to specify how you installed it. (If you edit your question to indicate this and leave a comment to this answer, I'll try to add information about how to remove that too.)

Eliah Kagan
  • 117,780
  • 2
    I tried the above, and it completed without error on Ubuntu 11, but after I can still run java -version and get back openjdk version "11.0.3" 2019-04-16 – John Little May 24 '19 at 14:17
30

To uninstall Oracle Java 7, just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.

sudo update-alternatives --display java

To check the setup before uninstalling Java.

Next, remove symlinks

(replace the word (version)with your Java version. DO java -version to get yours. So if your version is 1.7.0_03, you would type sudo update-alternatives --remove "java" "/usr/lib/jvm/jdk1.7.0_03/bin/java")

sudo update-alternatives --remove "java" "/usr/lib/jvm/jdk<version>/bin/java"
sudo update-alternatives --remove "javac" "/usr/lib/jvm/jdk<version>/bin/javac"
sudo update-alternatives --remove "javaws" "/usr/lib/jvm/jdk<version>/bin/javaws"

verify that the symlinks were removed

java -version
javac -version
which javaws

The next 2 commands must be type excatly perfectly to avoid permanently destroying your system.

cd /usr/lib/jvm
sudo rm -rf jdk<version>

Then do

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws

Then do

sudo vi  /etc/environment

Delete the line with JAVA_HOME 1



To uninstall OpenJDK (if installed). First check which OpenJDK packages are installed.

sudo dpkg --list | grep -i jdk

To remove openjdk:

sudo apt-get purge openjdk*

Uninstall OpenJDK related packages.

sudo apt-get purge icedtea-* openjdk-*

Check that all OpenJDK packages have been removed.

sudo dpkg --list | grep -i jdk

1Source:akbarahmed.com

Mitch
  • 107,631
18

You may not need to completely remove the OpenJDK to resolve your problem. Install the sun-java6 packages. Then use update-java-alternatives to switch to the Sun java packages.

If you do want to completely remove OpenJDK remove the default-jdk and/or default-jre packages. You may need to remove some java packages but most of them should be happy once you have the Sun JDK packages installed.

You may want to follow one of these cleanup tips once you are done removing packages.

BillThor
  • 4,698
  • I did the update-java-alternatives already. Should have mentioned that. I also did update-alternatives --config java. Now chrome and firefox tell me my java plugin is blocked because it's old... – CaldwellYSR Dec 02 '11 at 03:13
  • Run update-java-alternatives with the --plugin option and set it back to the original value. You can mix and match defaults with this tool. The browsers installed with 11.10 may be expecting sun-java7. – BillThor Dec 02 '11 at 03:24
8

Try this command:

java -version

If 1.6* comes then try:

sudo apt-get autoremove openjdk-6-jre

If 1.7* comes then try:

sudo apt-get autoremove openjdk-7-jre

Assuming that you don't have jdk in your system. If you have use this command:

sudo apt-get autoremove openjdk-`<version>`-jdk

replace with the version like we did it in previous example.

Aaron
  • 6,714
Pranit Bauva
  • 1,091
2

easier is to use synaptic.

click tab "sections" scroll down to java. simply select each item you see with green box and mark for complete removal. repeat for all all java sections and all files.

rob grune
  • 1,068
1

With JDK 18

sudo apt-get autoremove openjdk-18-jdk
Vy Do
  • 509
  • 1
  • 3
  • 13
1

Just remove all files of JDK or JRE, for example it's usually installed in this location:

/usr/lib/jvm/java-7-oracle

So remove all files resides in "java-7-oracle" folder with root permission and extract the latest JDK or JRE files in it. That's it now you would have the latest Java version Installed.

P.S. Your directory name may differ from java-7-oracle.

Reference: Official installation instruction from Oracle

1

Agreed with Eliah. "apt-get purge" command can remove those packages completely.

Assuming you have previously installed a copy of java-6-sun package, a followup will help getting rid of broken symbolic link:

sudo update-alternatives --config java

And then select a correct path which you want to link up as default Java calling path.

Then have a check on the version of Java with this command:

java -version
Eliah Kagan
  • 117,780
code_x747
  • 111