Gunnar Morling

Gunnar Morling

Random Musings on All Things Software Engineering

Gunnar Morling

Gunnar Morling

Random Musings on All Things Software Engineering

Building OpenJDK From Source On macOS

Posted at Nov 16, 2024

Every now and then, it can come in very handy to build OpenJDK from source yourself, for instance if you want to explore a feature which is under development on a branch for which no builds are published. For some reason I always thought that building OpenJDK is a very complex processing, requiring the installation of arcane tool chains etc. But as it turns out, this actually not true: the project does a great job of documenting what’s needed and only a few steps are necessary to build your very own JDK.

The following is a run-down of what I had to do to build JDK 24 from source on macOS 14.7.1. This is mostly for my own reference, check out the upstream documentation for a comprehensive description of the OpenJDK build, all requirements, build options, etc.

First, install the required tools:

  • A boot JDK, typically the previous version; I highly recommend to use SDKMan to do so:

    1
    
    sdk install java 23.0.1-tem
    
  • XCode, Apple’s development environment for macOS; the easiest way is to get it from the App Store. Unfortunately though, the current release 16.1 ships a broken version of clang which makes the JDK build fail. So you should either install 15.4 from Apple Developer, or apply the following patch before building OpenJDK which sidesteps that issue (at the price of building with fewer compiler optimizations):

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    git apply << EOF
    --- a/make/autoconf/flags-cflags.m4
    +++ b/make/autoconf/flags-cflags.m4
    @@ -337,9 +337,9 @@ AC_DEFUN([FLAGS_SETUP_OPTIMIZATION],
           C_O_FLAG_HIGHEST="-O3 -finline-functions"
           C_O_FLAG_HI="-O3 -finline-functions"
         else
    -      C_O_FLAG_HIGHEST_JVM="-O3"
    -      C_O_FLAG_HIGHEST="-O3"
    -      C_O_FLAG_HI="-O3"
    +      C_O_FLAG_HIGHEST_JVM="-O1"
    +      C_O_FLAG_HIGHEST="-O1"
    +      C_O_FLAG_HI="-O1"
         fi
         C_O_FLAG_NORM="-O2"
         C_O_FLAG_DEBUG_JVM="-O0"
    EOF
    
  • Autoconf:

    1
    
    brew install autoconf
    

With that, you should have everything in place for building OpenJDK:

  • Clone the project:

    1
    2
    
    git clone https://git.openjdk.org/jdk
    cd jdk
    
  • Run configure:

    1
    
    bash configure
    
  • Run the actual build:

    1
    
    make images
    
  • Rejoice:

    1
    2
    3
    4
    
    ./build/macosx-aarch64-server-release/jdk/bin/java --version
    openjdk 24-internal 2025-03-18
    OpenJDK Runtime Environment (build 24-internal-adhoc.gunnarmorling.jdk)
    OpenJDK 64-Bit Server VM (build 24-internal-adhoc.gunnarmorling.jdk, mixed mode)
    

And that’s it, you now have your own JDK build you can use for testing. Pretty easy, right? That said, if you still don’t feel like running this build by yourself, and if you’re on Linux rather than macOS, you also can check out the OpenJDK builds provided by Aleksey Shipilëv, which are provided for a variety of OpenJDK projects as well as target platforms.