Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A project builds with JDK 21 using maven.compiler.source=17 and target=17 and deploys to Java 17. In production it fails with NoSuchMethodError on a JDK method. Why, and what is the correct setting?

source and target only control which language features we may use and which bytecode version javac writes; they do not stop us calling methods that exist only in the JDK we compile with. Built on JDK 21, a call to a method added after Java 17 compiles fine and then fails on the Java 17 runtime with NoSuchMethodError. The correct setting is maven.compiler.release=17, which uses javac --release and checks every API call against the Java 17 platform; with the Spring Boot parent we set java.version instead, and we can add toolchains and the Enforcer requireJavaVersion rule for extra safety.

Why this question is asked

This bug is common because the build looks correct: the POM says 17, the jar's class files say 17, and the service starts on Java 17. Then a code path runs that uses a newer JDK method, and production fails. Interviewers ask it to see whether we understand the difference between the language level, the bytecode level and the class library we compile against. In this answer, we will recap what source and target do, reproduce the failure, see why the compiler let it through, fix it with release, and look at toolchains, the Spring Boot property and an Enforcer rule.

1A Quick Recap of source and target

The Java compiler, javac, has two old options for older platforms. -source says which version of the Java language to accept: with -source 17, javac rejects syntax from later versions. -target says which class file version to produce: with -target 17, it writes class files that a Java 17 JVM can load. Maven passes them from the properties maven.compiler.source and maven.compiler.target.

What neither option changes is the class library. When javac compiles a call such as list.getFirst(), it looks the method up in the JDK it is running on. On JDK 21, java.util.List has getFirst(), because Java 21 added it with sequenced collections. So the call compiles, whatever source and target say.

2Reproducing the Failure

Here is a small service built on JDK 21 with source and target set to 17, then run on Java 17.

XML
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
// OrderService.java
List<Order> orders = repository.findRecent();
Order newest = orders.getFirst(); // added to List in Java 21
$ mvn -v # Java version: 21.0.5
$ mvn package
# [WARNING] system modules path not set in conjunction with -source 17
# [INFO] BUILD SUCCESS
$ /opt/java17/bin/java -jar target/orders.jar
# java.lang.NoSuchMethodError: 'java.lang.Object java.util.List.getFirst()'

Notice the warning during the build. javac is telling us, in its own words, that it is compiling for 17 against a newer class library. Most builds print it and nobody reads it. The jar itself is fine as bytecode, it loads on Java 17, and it only fails when that line runs.

3Why the Compiler Let It Through

Each method call in a class file records the owner class, the method name and its descriptor (parameter and return types). javac found java.util.List.getFirst() in JDK 21's class library and wrote that reference. Nothing checks it again until the JVM executes it. Java 17's List has no getFirst(), so the JVM throws NoSuchMethodError at that moment.

So source and target together only guarantee that the syntax and the class file format fit Java 17. They promise nothing about the APIs we use. The same trap has caught people for years; a famous older case is ByteBuffer.flip(), whose return type changed in Java 9, so code compiled on JDK 9 or later with target 8 failed on Java 8.

What each setting checks
SettingLanguage levelBytecode versionAPI usage
source=17, target=17 on JDK 211717Not checked: JDK 21 library
release=17 on JDK 211717Checked against Java 17
Build on JDK 17 via toolchains1717Checked: real JDK 17

4The Fix: maven.compiler.release

Since Java 9, javac has a --release option that replaces both and also compiles against a stored description of that Java version's public API. The JDK ships these descriptions for older releases it supports, so JDK 21 knows exactly which classes and methods Java 17 has. Maven passes it through the maven.compiler.release property, or the release parameter of the compiler plugin.

XML
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
$ mvn package
# [ERROR] OrderService.java:[12,30] cannot find symbol
# [ERROR] symbol: method getFirst()
# [ERROR] location: variable orders of type java.util.List<Order>
// fixed code that works on 17:
Order newest = orders.get(0); // compiles and runs on 17

Now the error appears at build time, where it is cheap, instead of in production. We remove the source and target properties, since release replaces them; when release is set, the compiler plugin ignores the other two.

5With the Spring Boot Parent: Set java.version

If the project inherits from spring-boot-starter-parent, there is an even simpler rule. The parent defines a java.version property and uses it to set the compiler's release, so we only set java.version. Setting maven.compiler.source and target separately in such a project is a common mistake, because it suggests they control the build when release is what matters.

XML
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.7</version>
</parent>
<properties>
<java.version>17</java.version> <!-- the parent turns this into release 17 -->
</properties>

The Spring Boot version shown is an example. Check your parent's effective POM with mvn help:effective-pom to see the exact compiler configuration it produces.

6Extra Safety: Toolchains and requireJavaVersion

release checks the public API, but it does not run our tests on Java 17. If we want the build and tests to use the same JDK as production, Maven toolchains let the build use a specific JDK regardless of which one runs Maven. We describe the installed JDKs in ~/.m2/toolchains.xml and ask for version 17 with the toolchains plugin or the compiler plugin's jdkToolchain setting. On CI, running the build in an image with JDK 17 achieves the same.

We can also fail fast when someone builds with the wrong JDK. The Enforcer plugin's requireJavaVersion rule checks the JDK running Maven.

XML
<rules>
<requireJavaVersion>
<version>[17,)</version> <!-- Maven must run on Java 17 or later -->
</requireJavaVersion>
</rules>
# on JDK 11: [ERROR] Detected JDK version 11.0.24 is not in the allowed range [17,).
Step throughChoosing the compiler setting
  1. Project uses spring-boot-starter-parent?
    YesSet java.version onlyNoSet maven.compiler.release
  2. Must tests run on the production JDK?
    YesAdd toolchains or a CI image with that JDKNorelease alone is enough
  3. Add requireJavaVersionfail fast on a JDK that is too old

3 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Why does javac print 'system modules path not set in conjunction with -source 17'?

Because it notices that we asked for an older language level but are compiling against the current JDK's modules, which is exactly the situation that leads to this bug. The warning goes away when we use --release, which sets the right platform API automatically. Treating that warning as an error is a cheap safety net.

Can release check internal or non-public APIs?

No. --release only exposes the documented public API of that Java version, so code that uses internal classes such as sun.misc.Unsafe through the compiler may fail to compile with release even though it worked with source and target. That is usually a useful warning that the code depends on something that may change.

Is it safe to run a jar compiled for 17 on Java 21 or 25?

Generally yes. The JVM loads older class files, and the Java platform keeps strong backward compatibility. The risks are removed or restricted APIs and changed behaviour of internals, which is why we still run the tests on the new runtime before switching production.

8Conclusion

In this answer, we saw that source and target control only the language level and bytecode version, so code compiled on JDK 21 against JDK 21's class library can call methods that do not exist on Java 17 and fail with NoSuchMethodError at runtime. The fix is maven.compiler.release, or java.version with the Spring Boot parent, which checks API usage against Java 17. Toolchains let us build and test on the production JDK, and requireJavaVersion fails fast on the wrong one.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is that source and target do less than people think. Source sets the language level and target sets the class file version, but javac still compiles against the class library of the JDK it runs on, which is 21 here.

Then, if they want more

So if the code calls, say, List.getFirst(), which was added in Java 21, it compiles fine, the class files are valid Java 17 bytecode, and it fails on the Java 17 runtime with NoSuchMethodError when that line runs. javac even warns about it, the system modules path not set in conjunction with source 17 warning, which most people ignore.

The correct setting is maven.compiler.release=17, which maps to javac --release. It sets the language level and bytecode version and also compiles against a description of the Java 17 API, so the getFirst() call becomes a compile error. I would remove source and target once release is set.

With spring-boot-starter-parent, I would just set java.version, because the parent turns that into release. If we want tests to run on the same JDK as production, I would add Maven toolchains or build in a CI image with JDK 17.

And the Enforcer rule requireJavaVersion makes the build fail fast if someone runs it on a JDK that is too old.

About this answer

Last updated
12 November 2026
Versions it names
Java 17, Java 21, Java 9, Java 11
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 project builds with JDK 21 using maven.compiler.source=17 and target=17 and deploys to...": https://scrivio-interview-library.netlify.app/java/maven/b99a590781/