How to uninstall OpenJDK?

I don’t want OpenJDK installed on my Ubuntu 13.04 system, but trying to uninstall the openjdk-7-jre yields the following message:

$ sudo apt-get purge openjdk-7-jre
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  gcj-4.7-base gcj-4.7-jre gcj-4.7-jre-headless gcj-4.7-jre-lib gcj-jre gcj-jre-headless icedtea-6-jre-cacao icedtea-6-jre-jamvm libgcj-common libgcj13 libgcj13-awt
  openjdk-6-jre-headless openjdk-6-jre-lib
Suggested packages:
  fastjar gcj-4.7-jdk gcj-jdk libgcj13-dbg sun-java6-fonts fonts-ipafont-gothic fonts-ipafont-mincho ttf-telugu-fonts ttf-oriya-fonts ttf-kannada-fonts
  ttf-bengali-fonts
The following packages will be REMOVED:
  default-jre* icedtea-7-plugin* icedtea-netx* icedtea-plugin* libatk-wrapper-java* libatk-wrapper-java-jni* openjdk-7-jre*
The following NEW packages will be installed:
  gcj-4.7-base gcj-4.7-jre gcj-4.7-jre-headless gcj-4.7-jre-lib gcj-jre gcj-jre-headless icedtea-6-jre-cacao icedtea-6-jre-jamvm libgcj-common libgcj13 libgcj13-awt
  openjdk-6-jre-headless openjdk-6-jre-lib
0 upgraded, 13 newly installed, 7 to remove and 4 not upgraded.
Need to get 57,0 MB/57,2 MB of archives.
After this operation, 156 MB of additional disk space will be used.
Do you want to continue [Y/n]?

What do I need to do so that no new packages, of which I need none, will be installed when removing openjdk-7?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Great question, I was wondering this myself.

I found that you can use the following to remove the openjdk-7-jre on Ubuntu 13.04:

sudo apt-get autoremove openjdk-7-jre

Press ‘y’ and then press enter when prompted to confirm this change. This should also clean up all the additional dependency libraries that were installed with it.

I also found you can use the following command to perform additional clean up:

sudo apt-get purge openjdk*

If you use the following command:

java -version

You should no longer see the openjdk-7-jre installed!

Hope this helps. 🙂

Solution 2

You can use the following command:

sudo apt-get purge openjdk-8-jre openjdk-8-jre-headless openjdk-7-jre gcj-4.7-base gcj-4.7-jre openjdk-6-jre-headless

In this way no new packages will be installed. Here is my output for the above command:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'gcj-4.7-base' is not installed, so not removed
Package 'gcj-4.7-jre' is not installed, so not removed
Package 'openjdk-6-jre-headless' is not installed, so not removed
The following packages will be REMOVED:
  default-jre* icedtea-7-plugin* icedtea-netx* libatk-wrapper-java*
  libatk-wrapper-java-jni* libreoffice-base* minecraft-installer*
  openjdk-7-jre*
0 upgraded, 0 newly installed, 8 to remove and 6 not upgraded.
After this operation, 10,3 MB disk space will be freed.
Do you want to continue [Y/n]? n

But, I don’t suggest you to do this. In one day you will need for sure an OpenJDK (6 or 7).

Solution 3

To uninstall OpenJDK, you need to run these commands in your terminal.
You can get the terminal by using these keys, ctrl + shift + enter.

Remove Openjdk only:

$ sudo apt-get remove openjdk*

Remove Openjdk along with dependencies:

$ sudo apt-get remove --auto-remove openjdk*

Remove Openjdk and it’s configuration files:

$ sudo apt-get purge openjdk*

Remove Openjdk along with dependencies and it’s configuration files:

$ sudo apt-get purge --auto-remove openjdk*

Solution 4

Why this happens

This happens likely because another package on your system is depending on Java. While removing openjdk-7-jre, apt-get sees that if it uninstalls OpenJDK, some packages will have unmet dependencies. To solve this, it installs another package which provides Java.

To find out what is causing this, run these commands in a terminal:

packages=(default-jre default-jre-headless icedtea-6-plugin icedtea-7-plugin java-compiler java-jdk java-runtime java-runtime-headless java-sdk java-virtual-machine java2-jdk java2-runtime java2-runtime-headless java2-sdk java5-jdk java5-runtime java5-runtime-headless java5-sdk java6-jdk java6-runtime java6-runtime-headless java6-sdk java7-jdk java7-runtime java7-runtime-headless java7-sdk openjdk-6-jdk openjdk-6-jre openjdk-6-jre-headless openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless oracle-java7-bin oracle-java7-fonts oracle-java7-jdk oracle-java7-jre oracle-java7-plugin sun-java6-bin sun-java6-fonts sun-java6-jdk sun-java6-jre sun-java6-plugin)
for pkg in "${packages[@]}"; do
    apt_cache_out="$(apt-cache --installed rdepends "$pkg" | grep -E '^ [| ]\S')"
    if (( $? == 0 )); then
        echo -----------------------
        echo "$pkg"
        echo "$apt_cache_out"
    fi
done

Example output (shortened):

-----------------------
openjdk-7-jre
 |libreoffice-filter-mobiledev
 |libreoffice
 |libreoffice-writer
 |libreoffice-base
 |libreoffice-filter-mobiledev
 |libreoffice
 |libreoffice-writer
 |libreoffice-base

This indicates that all the LibreOffice packages are depending on openjdk-7-jre. As long as these packages are installed, OpenJDK cannot be uninstalled without installing an alternate Java automatically.

To remove openjdk-7-jre

If you want to remove openjdk-7-jre (without installing other packages), you will need to uninstall all packages which depend on it first.

From the example output, that will be:

sudo apt-get remove oracle-java7-installer libreoffice-filter-mobiledev libreoffice libreoffice-writer libreoffice-base libreoffice-filter-mobiledev libreoffice libreoffice-writer libreoffice-base

Solution 5

I notice you said you wanted to use Oracle’s JDK. If you want to install Oracle’s JDK instead, you can download that and extract it in the location of your choice.

Then you just use update-alternatives to tell your system where Java is installed. If you’re not sure about the syntax of update-alternatives, you can use the front-end for it, galternatives.

For example… I use Oracle’s JDK quite extensively here.

I extract it to /opt/java/.

Then, so all of Oracle Java’s binaries are the primary utilities for Java-related stuff, I do the following with update-alternatives:

for j in `ls /opt/java/bin` ; do update-alternatives --install /usr/bin/"$j" "$j" /opt/java/bin/"$j" 1 ; done

This takes all of the Oracle Java utilities that you’ve previously extracted, and
gives them the first priority, in case you have other versions of Java installed. (since Ubuntu allows you to have multiple versions of the same utility installed)

Solution 6

Along with the rest of answers – have you checked your .bashrc file to make sure that JAVA_HOME environment is not there? Removing that line should do the trick in freeing your Ubuntu from openjdk. Like an example:

 JAVA_HOME=/usr/local/java/jdk1.7.0_45
 export PATH=$JAVA_HOME:$PATH

If you see anything like that, then delete these two lines and reboot.

Solution 7

Since most of the questions mention a specific openjdk version, they might possible fail in the future

So I decided to create a bash script that detects all the openjdk versions and removes them

#script to remove any openjdk package and purge it, regardless of the version

for i in $(dpkg -l | grep openjdk | awk '{print $2}'); do
    echo removing "$i"
    sudo apt remove "$i"
    echo purging "$i"
    sudo apt purge "$i"
    echo -------
done

The above script you need to run it until it does not remove anything since I’m not listing the packages in the order of dependency

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply