'How to compile OpenJDK 11 on an M1 Macbook?

I want to compile the OpenJDK 11 for an M1 MacBook.

First, I tried to use the Zulu JDK, but I did not find out a way to compile it.

Then, I tried to try compile OpenJDK on an M1, but this reports an error when I run sh configure.

The error is:

configure: The tested number of bits in the target (64) differs from the number of bits expected to be found in the target (32) configure: error: Cannot continue.

So, how can I compile it?



Solution 1:[1]

tl;dr

I want to compile jdk in M1 MacBook.

Do not try to compile OpenJDK. Codebase is large and complicated. And no need, as others provide binaries & installers.

For a Mac using Apple Silicon rather than Intel x86-64, I suggest obtaining a distribution of Java 17 for macOS?/?AArch64, released 2021-09, from any of several vendors.

Or, use a build of earlier versions of Java ported to Apple Silicon by various vendors. For example, Azul Systems announced 2020-11 plans to ship builds of OpenJDK 8 and 11 for Apple Silicon, ARM-based Macs. I see downloads available now from that company for Java 11.0.13+8 on both Intel Macs and Apple Silicon Macs.

Details

Few of us ever compile the very large and complicated codebase of OpenJDK. Instead, we look to any of several vendors providing builds and installers.

Java 17 was released 2021-09. OpenJDK officially supports macOS on Apple Silicon: JEP 391: macOS/AArch64 Port. You have your choice of builds/installers from any of several vendors such as SAP, Red Hat/IBM, BellSoft, Azul Systems, Oracle, Adoptium/AdoptOpenJDK, Microsoft, Amazon, Pivotal, and more. These builds/installers will be based largely or entirely on the OpenJDK codebase.

FYI, Java 17 is the latest long-term support (LTS) version, to be supported for many years.

I myself am have been using first the early-access Java 17, and now the official Java 17 release, on my M1 MacBook Pro with IntelliJ 2021.x and Maven on Big Sur. Working well, no problems. Java 18 Early Access is now available for both Intel Macs and Apple Silicon Macs.

As commented, your specific error is likely due to you compiling for 32-bit ARM rather than 64-bit ARM used by Apple for their M1 chip.

If you insist on attempting to compile OpenJDK, I suggest examining the open-sourced tooling at Adoptium/AdoptOpenJDK. The tooling for building early-access builds at the OpenJDK site may also be open-sourced but I don’t know for certain.

Solution 2:[2]

I had install Android Studio Version, please see screenshot enter image description here

I was getting issues (install JAVA ) while building the Flutter Application, So I had installed JDK 17 but it was not compatible.

I had went AZUL

I installed first .dmg file. Please see screenshot enter image description here

It will solve the issue for building Gradle and APK issue with M1 Mac

Solution 3:[3]

You can build OpenJDK 11.0.12 on an M1 MacBook, with the help of Homebrew, by running:

brew install --build-from-source --verbose openjdk@11

Xcode is a prerequisite, you can download it from there https://developer.apple.com/download/more/ (Apple ID needed).

You probably also have to run:

xcode-select --install
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

I've tested it and it builds perfectly.

Solution 4:[4]

You can try it now with OpenJDK 11.0.15 source code. Previously, M1 CPU would be detected as ARM32 (macosx-arm) and that's why you saw that error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Anil Gupta
Solution 3
Solution 4 yoroto