This subproject illustrates how to leverage Gradle's Toolchains for JVM projects to use an early-access version of Java in a Gradle project.
This is a Java 23 Early Access project that's built with Java 21. Wait, what does that mean? This is a bit of a facetious statement. There's no trickery here. Remember that Gradle itself is a Java program, and we are free to use a different version of Java to run Gradle than the version of Java used to compile and run our program. In this project:
- The Gradle process runs on Java 21
- Gradle is our build tool. As such, we can facetiously say "we use Java 21 to build the project".
- The compilation process runs on Java 23
- Specifically, the
./gradlew compileJava
task will kick off a Java 23javac
process. Gradle refers to this pattern of creating a secondary Java process as "forking a Java process".
- Specifically, the
- The testing process runs on Java 23
- Specifically, the
./gradlew test
task will fork a Java 23 process to run the JUnit tests.
- Specifically, the
- The run process uses Java 23
- Specifically, the
./gradlew run
task will fork a Java 23 process to run the program (Thepublic static void main(String... args)
method) - The
run
task is provided by the application plugin)
- Specifically, the
Out of convenience and simplicity, it's best to use the same version of Java to run Gradle and to compile, test and run your program. So, why ever use two different versions? Out of necessity. Gradle itself cannot always run on new versions or work-in-progress versions of Java. These versions of Java are called Early Access versions. For example, in August 2024 the early access versions of Java were Java 23, 24, Leyden, Valhalla and more, and the latest version of Gradle was Gradle 8.9. Gradle 8.9 does not run on Java 23. So, at the time it was necessary to have a split-version project.
Friendly reminder: use the official OpenJDK site to stay up-to-date on the latest OpenJDK plans, like Java 21.
The Gradle mechanic to configure a split-version project is Gradle's Toolchains for JVM projects.
Follow these instructions to build and run the project.
- Pre-requisite: Java 21 and Java 23 early access
- Make sure that Java 23 is installed in a location known to Gradle
- Gradle can auto-detect installations of Java and the JDK
- Alternatively, you can specify a custom location
- Run the tests:
-
./gradlew test
-
- Run the program:
-
./gradlew run
- It will print the following:
Hello World! I am running in Java 23-ea
-
If you are using an early access version of Java then you will almost definitely be curious to use preview language
features. For a working example of how to configure Gradle to enable Java language preview features, see the
java-preview-features/
sibling project.
General clean-ups, TODOs and things I wish to implement for this project:
- DONE (UPDATE: I still like "toolchain" in the title. Let's make it long: "java-early-access-via-toolchain") Rename this to "java-early-access". I am generally favoring using toolchains everywhere now. So having a standalone
example project dedicated to toolchains is a bit redundant. This project is actually a great example of just "How do I
set up a Gradle project to work on an early access version of Java?". I think
java-early-access
is a better way to position this project thanjava-toolchain
because it describes a concrete use-case for differing between the Java version used for running Gradle vs the Java version used for compiling and running the program. - DONE Use a TOML Gradle version catalog