Running Java JDK natively on Apple M1 Macs (Apple Silicon)

I recently moved from an i5 2018 MacBook Pro to an M1 MacBook Air and I thought I’d see if I could keep everything native rather than using Rosetta 2.
Two items have popped up so far – there is no driver for my printer, and native Java requires some hoops depending on your needs.
Before I dig any deeper, you can absolutely go straight to the Azul download site and download the .dmg for Zulu Java JDK; I didn’t want to go with this, I usually use Homebrew to manage my apps, and as I do Java development, want to have multiple Java versions installed and managed.

Homebrew fails to install a native JDK, and instead tries to install an x86 compiled version which errors on install if you don’t have Rosetta 2 installed. I’ve raised a request for this as Homebrew should ideally offer the native Java JDK option.

I went hunting and found that SDKMAN had an enhancement request to support Apple Silicon, and that it was available in the 5.10.0 release.

SDKMAN is a great little tool to allow you to quickly install multiple JDKs and can be coupled nicely with jenv to allow you to easily switch your primary JDK.

Installing and configuring SDKMAN

Follow normal SDKMAN install instructions, basically just run curl -s "https://get.sdkman.io" | bash

Next, update config field sdkman_rosetta2_compatiblefor SDKMAN to set to false vi ~/.sdkman/etc/config

sdkman_auto_answer=false
sdkman_auto_selfupdate=false
sdkman_insecure_ssl=false
sdkman_curl_connect_timeout=7
sdkman_curl_max_time=10
sdkman_beta_channel=false
sdkman_debug_mode=false
sdkman_colour_enable=true
sdkman_auto_env=false
sdkman_rosetta2_compatible=false

Restart terminal so that the updated config gets set.

Installing a JDK

In case you missed it, make sure you restart your terminal, if you don’t then you’ll get a longer list when you run sdk list java, you should see a short list (just Azul and Bellsoft at the time of writing)

Below is an example command to install Zulu 16.0.1 but obviously you can change to your preferred flavour based on the sdk list java output.

sdk install java 16.0.1-zulu

You can then run java --version to check that it worked the way you expected…

java --version
openjdk 16.0.1 2021-04-20
OpenJDK Runtime Environment Zulu16.30+15-CA (build 16.0.1+9)
OpenJDK 64-Bit Server VM Zulu16.30+15-CA (build 16.0.1+9, mixed mode)

Gotchas

If you did what I did and didn’t restart your terminal so the new config got picked up then you would have received an error when running java as below:

java
exec failed: Error Domain=NSPOSIXErrorDomain Code=86 "Bad CPU type in executable" UserInfo={NSLocalizedFailureReason=Failed to execute /Users/grahamrb/.sdkman/candidates/java/current/bin/java: Bad CPU type in executable}

This means you accidentally install the x86 version.
Stop now and restart your terminal so the config gets picked up. Next remove the cache of the JDK otherwise it’ll just reinstall the x86 version (I made this mistake).
rm ~/.sdkman/archives/*.zip

Finally, follow the JDK install instructions above.

Photo by Nathan Dumlao on Unsplash