Switch Java versions on Mac OS X


Note: At the time of writing, the OS in question is Mac OS X High Sierra 10.13.6.

Mac OS X contains a rudimentary amount of developer tools underneath the shiny user interface. These packaged tools get outdated very fast, and developers are left scrambling to get the updated tools installed. Java, as a matter of fact, doesn’t come installed by default nowadays.

My weapon of choice: Brew.

Back to the topic. When updating my server and Invoice Ninja settings, I had to connect to the database to tweak a value in the currency table. I didn’t want $ to appear as the currency symbol for USD. I wanted USD to appear instead. This will lessen the confusion about which currency is used in the invoice. In Singapore, the $ sign is used locally as the currency symbol.

Actually…

However, I was not able to connect to the MySQL database via SSH tunnel on Sequel Pro. The error message was something about ‘root’@’localhost’ cannot be connected. The same error message came up on MySQL Workbench.

I scoured the online world for information, but after an hour or so, I decided to switch to another database remote management app and hoped that it was just the apps I relied on for years had finally gone out of date. I chose DBeaver as the alternative.

A standing beaver as DBeaver logo
Beavers are cute and cuddly creatures. Hopefully, so is DBeaver.

DBeaver currently has an requirement of Java 8. I had Java JRE 7 installed before and I had kept a Java 8 installation up to date. But, DBeaver didn’t detect it. So didn’t the system:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
    1.7.0_80, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home

So apparently, Oracle’s official installer installed Java 8 in another location. One way is to symlink the Java 8 installation into /Library/Java/JavaVirtualMachines.

Java Programming Language logo
A cup of joe would be appreciated after having to spend the entire day shimming down a rabbit hole.

Aha!

Another way is to use Brew do everything. Well, almost everything…

$ brew install java # This installs the latest Java version. At the moment, it is Java 10.
$ brew cask install homebrew/cask-versions/java8 # This installs Java 8.

I couldn’t find Java 7 and 9 in Brew. They were removed, I think.

These commands will install both Java 10 and 8 in the /Library/Java/JavaVirtualMachines/ folder.

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    10.0.2, x86_64:	"Java SE 10.0.2"	/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    1.8.0_181, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
    1.7.0_80, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home

What’s left to do is to establish a simple switch so that we can change the JVM version as needed. In Mac OS X, this can be done by setting JAVA_HOME variable in the terminal environment. Jeremy Zhang had provided his setup in Stack Overflow which I think is simple enough, and as adapted:

# In ~/.bash_profile:
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"

# In Terminal:
$ source ~/.bash_profile
$ j10
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

Nope. Still not done yet…

Back to the rabbit hole, I have yet to find out why ‘root’@’localhost’ is refused over SSH. Instead, for the time being, before I connect to the database, I create a separate SSH tunnel with the port forwarded, and connected from there.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.