Quick-tip: switch to Java 8 SDK on Mac OS X (2018)

java
coding
mac-os-x
Tags: #<Tag:0x00007fbc4b2c4b38> #<Tag:0x00007fbc4b2c49f8> #<Tag:0x00007fbc4b2c48b8>

#1

Java 8 - needed

For one reason or another I need Java 8.

Situation:

  • Oracle Java 10 on the Mac (not OpenJDK)
  • Want: Oracle Java 8 (not OpenJDK, as default)

Here is quick way to switch the SDK from Java 10 to Java 8, without breaking stuff.

➜  java -version
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

Good. Now… I use homebrew’s {1} cask {2} as an Apps manager for Mac OS X. I don’t want to deal with the download and installation manually. Furthermore this can be done with automation tools, like Ansible {3}. Because Macs die quite often. The hardware is by far less reliable than it used to be in the era of Powerbooks and PPC.

➜  brew tap caskroom/versions
==> Tapping homebrew/cask-versions
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-versions'...
remote: Counting objects: 212, done.
remote: Compressing objects: 100% (206/206), done.
remote: Total 212 (delta 15), reused 41 (delta 5), pack-reused 0
Receiving objects: 100% (212/212), 84.48 KiB | 1.28 MiB/s, done.
Resolving deltas: 100% (15/15), done.
Tapped 192 casks (231 files, 315.9KB).
➜  druid-0.12.1 brew cask install java8
==> Caveats
This Cask makes minor modifications to the JRE to prevent issues with
packaged applications, as discussed here:

  https://bugs.eclipse.org/bugs/show_bug.cgi?id=411361

If your Java application still asks for JRE installation, you might need
to reboot or logout/login.

Installing java8 means you have AGREED to the license at
  https://www.oracle.com/technetwork/java/javase/terms/license/index.html

==> Satisfying dependencies
==> Downloading http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-macosx-x
######################################################################## 100.0%
==> Verifying checksum for Cask java8
==> Installing Cask java8
==> Running installer for java8; your password may be necessary.

I think we get the point of this… Where is the SDK?

➜  /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    10, x86_64:	"Java SE 10"	/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
    1.8.0_181, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home

Cool. Now here is some magic with export for the ZSH / Bash session

➜  /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
➜  export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
➜  java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

Check. Next configure your Eclipse or IDEA and get going.

Example: Druid

Druid needs Java 8 {4}. Sadly they don’t point that out well enough… different story. Developers… doing their thing.

Essentially I just run the export in a terminal, before I start the component of the software.

➜  druid-0.12.1 curl -X 'POST' -H 'Content-Type:application/json' -d @quickstart/wikiticker-index.json localhost:8090/druid/indexer/v1/task
{"task":"index_hadoop_wikiticker_2018-08-07T11:20:17.777Z"}%

Results

  • No manual install steps are required
  • We can have Java 8 and Java 10 in parallel
  • We can switch SDKs easily
  • We can enumerate the installed SDKs with a single command
References

{1} Homebrew for Apple Mac OS X

{2} Cask for Homebrew

{3} Ansible and Homebrew - ypu may want your dev environment replicate-able.

{4} Druid install, quick start