java - How to compile OpenJDK 11 on an M1 Macbook? - Stack Overflow
macos - How to handle java apps lacking ARM support using java SDK on M1 Mac? - Ask Different
How to install java on Mac m1?
Provide JDK/JRE 8 and 11 builds for Apple Silicon (M1/ARM/aarch64)
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.
Update: As of 2023-11, I would suggest the current version 21 for deployment, as it is the latest LTS version. Also, you can now obtain JDK binaries and installers from many vendors, including Amazon, Microsoft, Azul Systems, BellSoft, Oracle, Adoptium, SAP, IBM, Red Hat, Linux apt-get, FreeBSD Ports and Packages, and more. Most of these provide editions of their JDK products for both Intel Macs and Apple Silicon Macs.
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.
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.
The issue here is that you are calling a C library via jogamp The C library provided (I think OpenGL) is Intel only.
Oracle's JDK and JRE are not universal (see the downloads page) i.e. they only do one architecture. Similarly the OpenJDKs are not universal.
Thus to run on ARM and Intel you need two different JREs
Pure Java code should run on any operating system (Linux, Solaris, Windows, macOS and others) and on any architecture e.g. Intel, Arm, SPARC. So you only need one jar for all.
The C code could be built universal and your app could be packaged to include both JREs and the launcher (i.e. executable file given in Info.plist) works out from the architecture which JRE to call)
Java 8 worked as it is Intel and so the whole process ran under Rosetta
If you install the ARM JDK and find it breaks some app using e.g. gluegen, the simplest solution is to install the Intel JDK.
In particular, if you installed the ARM JDK to the default location using the disk image from Oracle's site, you can do precisely the same thing with the Intel JDK. This will effectively overwrite the ARM JDK.
Don't try simply installing the (Intel) JRE on top of the mess. That does NOT work - the JRE built-into the ARM JDK somehow persists. (And following Oracle's instructions for deleting Java before re-installing the JRE won't help either.)
According to Oracle's site, it's possible in theory to install multiple JDK's each in their own location, and switch between them. That would probably also work, but I haven't tried it.
A command line approach (thanks to the Homebrew team and the hard work of @vladimir-kempik and other openjdk contributors on the JEP-391 branch)
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install OpenJDK
brew install openjdk
Verify it's installed:
$(brew --prefix openjdk)/bin/java --version
Verify it's for the arm64 hardware:
file $(brew --prefix openjdk)/bin/java
# /opt/homebrew/opt/openjdk/bin/java: Mach-O 64-bit executable arm64
Note: To install openjdk system-wide, follow the on-screen instructions provided by Homebrew.
Yes.
On this page: AdoptOpenJDK Latest Releases you can select 'macOS' from the 'Operating System' dropdown, and then from 'Architecture', it's currently only x64, but soonish there should be AArch64 or ARM64 (those are usually the shortcodes for 64-bit ARM). Possibly, as Apple no doubt has a bunch of extensions built into their M1 designs, and Apple gets its own.
If you instead leave Operation System on 'any', you'll note aarch64 is in there, and this gets you to a Linux release for ARM processors. That (probably) won't run on macOS on M1 hardware, but that's 95% of the work already done.
So: It's not there yet, but note that JDKs for ARM have been available for more than decade, and whilst JDK 15 has dropped support for a bunch of exotic OS/architecture combinations (such as Solaris), ARM development has always remained at least partially relevant (even if so far it's mostly an Oracle commercial license offering). That is to say: It should not be a herculean effort to create an adoptopenjdk release that runs on M1s natively, so presumably, it will happen. But, it's an open source effort, so if you're anxious, by all means, read up and contribute :)
Apple has not given any details on this architecture whatsoever until November 10th 2020, unless you bought a development kit box for it (a Mac Mini with an A14 chip, which isn't an M1 chip, but close enough I guess), and signed a big NDA.
As a rule, open source projects will run as fast as possible in the opposite direction if you wave an NDA around, so if you dislike this state of affairs, I don't think it's wise to complain to adoptopenjdk or other packagers and open source projects about it :)
Fortunately, now it's out, and an NDA is no longer required. My assumption is that the ARM branch of the OpenJDK source code + the macOS bits that already exist for the macOS x64 release can be combined rather easily once someone with some familiarity with the OpenJDK source code has an M1-based macOS system to test it on, which should mean an adoptopenjdk macos-aarch64 release should be here within the month.
But, open source. You didn't pay them, you have no contract, and they don't owe it to you. Donate to the effort or contribute a pull request if you want it to go faster.
UPDATE:
- Azul's M1 OpenJDK builds
- Microsoft's (yes, really) GitHub source repo for an early access OpenJDK16 build for macOS on AArch64. Note that Microsoft's been working on the OpenJDK branch of AArch64 (for ARM-based Windows 10) for a while, which goes back to: A lot of the hard work was already done.