How to remove obsolete JDKs on MacOSX
A Java developer usually maintains on his machine a collection of different versions of Java JDKs to compile and test his applications.
After some time we discover that we want to remove obsolete JDKs after having added other versions.
Unfortunately, on MacOSX there is no an easy way to do that.
So these are the steps to perform a clean removal of an existing JDK.
First of all we would like to know how many JDKs are installed on our machine. To do that we open a terminal and type:
pkgutil --pkgs|grep com.oracle
on my machine, as an example, I get:
com.oracle.jdk7u79
com.oracle.jdk8u112
com.oracle.jdk8u31
com.oracle.jdk8u65
com.oracle.jdk8u77
com.oracle.jre
Let’s remove version 77 of JDK 8. First of all we need to know where the JDK files are located. To obtain this info:
pkgutil --info com.oracle.jdk8u77
I get:
package-id: com.oracle.jdk8u77
version: 1.1
volume: /
location: Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk
install-time: 1459414940
Very well, now we know that we must remove all files under
/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk
and we do that with root permissions:
sudo rm -R /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk
Ok, now we must only remember to delete the package information:
sudo pkgutil --forget com.oracle.jdk8u77
To confirm that we effectively delete the package, we can re-issue
pkgutil --pkgs|grep com.oracle
Finally, to add another JDK, it is sufficient to download the image from OpenJDK or Oracle site, and install it like any other MacOSX package.
(A small Gist with all the needed commands is available here)