How to properly install / uninstall Java on macOS
It's a common task to install multiple versions on macOS. So what are the best practices while doing all these?
Firstly, you may have installed Oracle Java 8, which is the latest version that I can get for macOS installation. Weird enough.
So let's uninstall it:
1sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
2sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
3sudo rm -fr ~/Library/Application\ Support/Oracle/Java
And we can use macOS's tool to check:
1$ /usr/libexec/java_home -V
2The operation couldn’t be completed. Unable to locate a Java Runtime.
3Please visit http://www.java.com for information on installing Java.
Now we're going to use brew
which is kind of the de facto package manager for Mac.
1# Check what java packages brew has
2$ brew search java
3==> Formulae
4app-engine-java java javacc jslint4java pdftk-java
5google-java-format java11 javarepl libreadline-java
6==> Casks
7charles-applejava eclipse-java eclipse-javascript java-beta java6 oracle-jdk-javadoc
8
9
10# Let's dive deeper into java11
11$ brew info java11
12openjdk@11: stable 11.0.12 (bottled) [keg-only]
13Development kit for the Java programming language
14https://openjdk.java.net/
15Not installed
16From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk@11.rb
17License: GPL-2.0-only
18==> Dependencies
19Build: autoconf ✘
20==> Caveats
21For the system Java wrappers to find this JDK, symlink it with
22 sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
23
24openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
25because this is an alternate version of another formula.
26
27==> Analytics
28install: 58,829 (30 days), 127,848 (90 days), 399,054 (365 days)
29install-on-request: 24,657 (30 days), 51,726 (90 days), 179,484 (365 days)
30build-error: 0 (30 days)
31
32
33# And install java11
34$ brew install java11
35...
36For the system Java wrappers to find this JDK, symlink it with
37 sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
38
39openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
40because this is an alternate version of another formula.
41
42If you need to have openjdk@11 first in your PATH, run:
43 echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc
44
45For compilers to find openjdk@11 you may need to set:
46 export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"
47
48==> Summary
49🍺 /usr/local/Cellar/openjdk@11/11.0.12: 679 files, 297.9MB
50
51
52# The java formula is keg-only, which means it is installed in /usr/local/Cellar but not linked into
53# places like /usr/local/bin or /Library/Java/JavaVirtualMachines/ (macOS /usr/bin/java wrapper).
54# As per the output from `brew install java11`, in order for macOS /usr/bin/java wrapper to find the installed JDK
55# we manually create a symbolic link.
56$ sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
57
58
59# Check the version
60$ java --version
61
62openjdk 11.0.12 2021-07-20
63OpenJDK Runtime Environment Homebrew (build 11.0.12+0)
64OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode)
65
66
67# And by macOS' wrapper tool
68$ /usr/libexec/java_home -V
69/usr/libexec/java_home -V
70Matching Java Virtual Machines (1):
71 11.0.12 (x86_64) "Homebrew" - "OpenJDK 11.0.12" /usr/local/Cellar/openjdk@11/11.0.12/libexec/openjdk.jdk/Contents/Home
72/usr/local/Cellar/openjdk@11/11.0.12/libexec/openjdk.jdk/Contents/Home
So now we can use this macOS' wrapper tool to switch versions, if you have more than one, say java8 + java11.
This can be easily added into your env, say ~/.profile
to make it enable java 11 by default.
1alias java11="export JAVA_HOME='/usr/libexec/java_home -v 11'; java --version"
2alias java8="export JAVA_HOME='/usr/libexec/java_home -v 1.8'; java --version"
3
4java11
Ref: