development · Java · tips and tricks

JDK on OS X: Bad CPU type

In 2020, Apple started the transition from Intel processors (x86) to Apple silicon (ARM-based M1 chips). In response to the new needs of ARM-based M1 OSx machine owners, new JVM builds were made available (JEP 391).

When setting up the environment, OSx users who rely on JDK might come across the following error:

$ mvn clean install
/usr/local/Cellar/maven/3.8.3/libexec/bin/mvn: line 194: /Library/Java/JavaVirtualMachines/jdk-19/Contents/Home//bin/java: Bad CPU type in executable
/usr/local/Cellar/maven/3.8.3/libexec/bin/mvn: line 194: /Library/Java/JavaVirtualMachines/jdk-19/Contents/Home//bin/java: Undefined error: 0

If this is your case, you’re using a JDK build that doesn’t match your machine’s architecture.

Picking the right JDK

When downloading a JDK, you should find two download options for Mac machines: macOS / AArch64 and macOS / x64. If downloading Java 20 early access, you should get:

OpenJDK 20 download options (2022, Jul)

Double-check which option you should download, by clicking on the Apple logo in your menu bar -> About This Mac.

  • If you have an Intel processor, download JDK macOS/x64.
  • If you have an Apple M1 processor, download macOS/AArch64.

Unzip and install the downloaded JDK as you usually do. Personally, I prefer to download and place all my JDK options under /Library/Java/JavaVirtualMachines/. Based on the task’s goals, I export the JAVA_HOME variable pointing to the version/vendor I need. For JDK 20, this is how it goes:

$ tar -xzvf ~/Downloads/openjdk-20-ea+7_macos-x64_bin.tar.gz -C /Library/Java/JavaVirtualMachines/
$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/
$ java -version
openjdk version "20-ea" 2023-03-21
OpenJDK Runtime Environment (build 20-ea+7-379)
OpenJDK 64-Bit Server VM (build 20-ea+7-379, mixed mode, sharing)

If you want this JVM configuration to be persistent, remember to add the JAVA_HOME variable in your ~/.bash_profile or ~/.profile file.

I hope this article is useful for those having doubts or issues when setting up a Mac development environment. If you’re into understanding the details about AArch64 + Java and JVMs, check the article Java on Arm: The AArch64 hardware, software, cloud, and JDK.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s