Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

A colleague configures the maven-compiler-plugin with source and target 17 but the code uses Java 21 APIs and still compiles on a JDK 21 machine, then fails on Java 17 at runtime. Why, and what is the correct setting?

source and target only control the language level the compiler accepts and the bytecode version it writes; they do not restrict which JDK APIs the code can call. Compiling on JDK 21 with target 17 therefore compiles against JDK 21's class library, so a call to a Java 21 method such as List.getFirst() compiles and then fails on Java 17 with NoSuchMethodError. The correct setting is release, maven.compiler.release=17, which compiles against the Java 17 platform API; toolchains can pin the JDK used for compilation, and CI should also test on the target runtime.

Why this question is asked

Many teams build on the newest JDK while still deploying to an older runtime, for example because some servers have not been upgraded. That is reasonable, but only if the compiler stops us from using APIs the older runtime does not have. Interviewers ask this because the difference between source, target and release looks like a detail, yet it explains a class of production failures that pass every build. In this answer, we will recap what each option controls, reproduce the failure, fix it with release, look at toolchains as a complementary tool, and set the CI rule that closes the gap.

1A Quick Recap: Language Level, Bytecode Version and API

When javac compiles code, three separate things are involved. The language level decides which syntax and language features are allowed, such as records or pattern matching; that is what -source sets. The bytecode version decides which JVM versions can load the class files; -target sets it, and a class file compiled for 17 has version 61 and can run on Java 17 and later. The platform API is the set of JDK classes and methods the code compiles against, such as java.util.List and its methods.

The catch is that -source and -target say nothing about the third. Without other options, javac compiles against the class library of the JDK that is running it. So JDK 21 with -source 17 -target 17 produces Java 17 bytecode that may call methods which only exist in Java 21.

What each compiler option controls
OptionLanguage levelBytecode versionPlatform API checked
-source 17yesnono
-target 17noyesno
--release 17yesyesyes: Java 17 API only
Toolchain JDK 17via the JDK usedvia the JDK usedyes: it is the real JDK 17

2Reproducing the Failure

Here is the colleague's configuration and a line of code that uses an API added in Java 21. List.getFirst() comes from the sequenced collections added in Java 21.

XML
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
// OrderService.java
Order first = orders.getFirst(); // List.getFirst() exists only since Java 21
$ mvn package # running on JDK 21
[WARNING] system modules path not set in conjunction with -source 17
[INFO] BUILD SUCCESS # compiles: JDK 21's List has getFirst()
$ java -jar app.jar # on a Java 17 runtime
Exception in thread "main" java.lang.NoSuchMethodError:
'java.lang.Object java.util.List.getFirst()'

The build succeeds because javac checks the call against JDK 21's List, where getFirst() exists, and writes version 61 class files that Java 17 happily loads. At runtime, Java 17's List has no getFirst(), so the first call fails with NoSuchMethodError. javac even prints a warning that the system modules path is not set together with -source, which is its way of saying it is not checking the API of Java 17, but warnings like this are easy to miss in a long build log.

3The Fix: release

Since Java 9, javac has a --release option. It sets the language level and the bytecode version, and also compiles against a built-in description of the public API of that Java version, which every JDK ships for the releases it supports. With --release 17, JDK 21 compiles as if the class library were Java 17's, so a call to a Java 21 method is a compile error. In Maven, the compiler plugin exposes it as the release parameter or the maven.compiler.release property.

XML
<properties>
<maven.compiler.release>17</maven.compiler.release> <!-- replaces source and target -->
</properties>
$ mvn package # still on JDK 21
[ERROR] OrderService.java:[12,24] cannot find symbol
[ERROR] symbol: method getFirst()
[ERROR] location: variable orders of type java.util.List<Order>
[INFO] BUILD FAILURE # caught at compile time, not in production

The fix is a one-line change, and source and target should be removed so nobody wonders which setting wins. The failure now appears where it is cheap to fix: in the build, with the exact line. For Spring Boot projects using the starter parent, setting the java.version property feeds the release setting, so check how the parent in use maps it.

4Toolchains: Pinning the Compiling JDK

Maven toolchains solve a related but different problem. They let the build use a specific JDK for compiling and testing, independent of the JDK that runs Maven itself. A toolchains.xml file on each machine lists the installed JDKs, and the build declares which version it needs; the compiler and Surefire plugins then use that JDK.

With a Java 17 toolchain, compilation happens with a real JDK 17, so the API check is automatic, and tests also run on Java 17, which catches behaviour differences that release cannot, such as a bug fixed only in a newer JDK. The cost is that every developer machine and CI agent must have that JDK installed and listed. For most projects, release is enough for compiling, and toolchains are worth it when tests must run on an exact runtime version.

5Closing the Gap in CI

release guarantees the API; it does not guarantee behaviour. JDK versions differ in garbage collector defaults, TLS settings, locale data and bug fixes. So the CI pipeline should run the tests on the same Java version as production, Java 17 here, either with a toolchain or by running the test stage on a Java 17 image. A matrix build that also runs on Java 21 is useful for preparing the upgrade, but the gate that protects production is the one that uses the production runtime.

6Follow-up questions

Can release be used to compile code that uses internal JDK classes?

No. release only exposes the documented public API of that Java version, so code using internal classes such as sun.misc.Unsafe or packages hidden by the module system may fail to compile with it. That is usually a good thing to discover, but for code that really needs it, we would compile with the matching JDK through a toolchain instead.

What happens if the runtime is newer than the target?

That is fine. Class files compiled for Java 17 run on Java 21 and later, because the JVM is backward compatible for bytecode. Only the reverse direction, newer bytecode or APIs on an older runtime, fails, with UnsupportedClassVersionError for the bytecode or NoSuchMethodError and NoClassDefFoundError for the API.

Does release work with Java 8 targets?

Yes. A modern JDK supports release values down to an older minimum that varies by JDK version, so check the version in use; recent JDKs have dropped support for compiling to the oldest releases.

7Conclusion

In this answer, we saw that source sets the language level and target the bytecode version, but neither limits the JDK API, so compiling on JDK 21 with target 17 lets code call Java 21 methods that fail with NoSuchMethodError on Java 17. The release option, maven.compiler.release, compiles against the exact API of the chosen version and turns that failure into a compile error. Toolchains pin the JDK used for compiling and testing, and CI should run the tests on the production runtime version to catch behaviour differences too.

What to say in the interviewAbout 90 seconds aloud

Open with

source and target do less than people think. source sets the language level, which syntax the compiler accepts, and target sets the bytecode version, so the classes load on Java 17.

Then, if they want more

Neither restricts the platform API: javac compiles against the class library of the JDK it runs on.

So on JDK 21 with target 17, a call like List.getFirst(), which was added in Java 21, compiles fine, produces Java 17 bytecode, and then fails on a Java 17 runtime with NoSuchMethodError the first time that line runs. javac even warns that the system modules path is not set together with source, but that is easy to miss.

The correct setting is release, so maven.compiler.release set to 17, or the release parameter of the compiler plugin. It sets language level and bytecode, and also compiles against a description of the Java 17 public API that the JDK ships, so the same code fails at compile time with cannot find symbol. I would remove source and target entirely.

If tests must also run on the exact runtime, I would add Maven toolchains, which let the build compile and test with a specific installed JDK independent of the one running Maven. And CI should run the tests on the production Java version, because release checks the API but not behaviour differences between JDKs.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 17, Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Maven guides
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A colleague configures the maven-compiler-plugin with source and target 17 but the code...": https://scrivio-interview-library.netlify.app/java/maven/d7d74fe858/