12

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?

Moritz
  • 1,954
  • 2
  • 18
  • 28
rainbowecho
  • 145
  • 1
  • 1
  • 9
  • 1
    Why do you want to? It is a very large and complex software base and others have already done the work. – Thorbjørn Ravn Andersen Jul 13 '21 at 08:17
  • 3
    Why do you want to build it yourself? There isn't even an AdoptOpenJDK build yet, so hacking this together on your own seems like asking for trouble. Fwiw the error most likely indicates you're trying to build for 32-bit ARM, m1 is ARM64. – somethingsomething Jul 13 '21 at 08:17
  • If the reason behind your question (or others like me coming here) is simply since Oracle does not have an M1 version of the jdk11, just download the linux version for arm (ARM 64 Compressed Archive). it worked for my M1 machine. Here's the link: https://download.oracle.com/otn/java/jdk/11.0.16%2B11/b8c39bd7f0064c2aa24e6002b391bd0f/jdk-11.0.16_linux-aarch64_bin.tar.gz – bladefist Jul 25 '22 at 15:03
  • https://stackoverflow.com/questions/68358505/how-to-compile-openjdk-11-on-an-m1-macbook – Wahab Khan Jadon Oct 04 '22 at 07:18

4 Answers4

14

tl;dr

target (64) differs … number of bits … in the target (32)

Apple Silicon chips for macOS (M1, M2) are all 64-bit architectures. Do not compile for 32-bit.

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. If your goal is to be productive in Java programming, then compiling the Java JDK toolset is not the best use of your time.

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.

Vendors such as Azul Systems provide Apple Silicon builds for the current version 19 as well as the long-term support (LTS) versions 8, 11, and 17. Java 17 is the latest LTS version, to be supported for many years. So I would suggest Java 17 for deployment.

I myself have been using Java 17, 18, and 19, as well as early-access 20, on a couple of M1 MacBook Pro laptops with IntelliJ and Maven on Big Sur & Monterey. Working well, no problems.

Compiling Java

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

If you are curious about, or have an academic interest in compiling Java from the OpenJDK codebase, I suggest examining the open-sourced tooling at Adoptium (formerly AdoptOpenJDK). The tooling for building early-access builds at the OpenJDK site may also be open-sourced but I don’t know for certain.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    Thank you very much! I will take some think – rainbowecho Jul 20 '21 at 10:35
  • 1
    With all respect, "Do not try something" is the worst advice possible. It's really complicated to build OpenJDK, agreed. But the complexity should not stop curiosity. – Sergei Rybalkin Jan 26 '22 at 11:21
  • @SergeiRybalkin I agree with your last sentence. That’s why I gave a couple of tips at the end of the Answer. – Basil Bourque Jan 26 '22 at 12:59
  • @BasilBourque Do you suggest using Rosetta 2? instead of installation of the JDK for Silicon specifically – Dr.jacky Feb 13 '23 at 22:23
  • 1
    @Dr.jacky I don't know what you mean. While I have heard that Java for Intel Macs runs well under [Rosetta 2](https://en.wikipedia.org/wiki/Rosetta_(software)#Rosetta_2), **there is no need**. For optimal performance, on a Mac with Apple Silicon use a build of Java compiled for Apple Silicon (ARM, Aarch64). For a Mac with Intel chips, use a build of Java compiled for Intel. Vendors such as Azul Systems provide Apple Silicon builds for the current version 19 as well as the Long-Term Support (LTS) versions 8, 11, and 17. – Basil Bourque Feb 14 '23 at 00:41
4

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.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • Need to compile jdk in debug mode. What could be the possible option to add? @ortamala – vkm May 10 '23 at 17:41
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

Anil Gupta
  • 1,155
  • 1
  • 19
  • 26
1

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.

yoroto
  • 97
  • 1
  • 2