Quick revision
The short answer
We compile shared libraries with --release 17, the oldest Java version any consumer runs, so the class files load everywhere and javac also refuses calls to APIs that do not exist in Java 17. To use newer APIs where they exist, we either ship a multi-release JAR with extra class versions for 21 or 25, or detect the feature at run time, and we raise the baseline on a published schedule as services upgrade.
What to remember
In this answer, we saw that the bytecode baseline is the class file version we compile to, and that the right choice for libraries is the oldest supported Java version, set with --release so javac also checks API usage. We looked at multi-release JARs for using newer APIs where they exist, run-time detection as a narrower alternative, and a published schedule for raising the baseline as services upgrade.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We compile shared libraries with --release 17, the oldest Java version any consumer runs, so the class files load everywhere and javac also refuses calls to APIs that do not exist in Java 17. To use newer APIs where they exist, we either ship a multi-release JAR with extra class versions for 21 or 25, or detect the feature at run time, and we raise the baseline on a published schedule as services upgrade.
Why this question is asked
A shared library is code that many services pull in, so its bytecode level decides which services can use it at all. Pick a level too high and the services still on Java 17 fail at startup; pick it too low forever and the library can never use the better APIs that came later. Interviewers ask this to see whether we understand class file versions, the difference between --release and -source/-target, and the tools Java gives us for supporting several versions at once. In this answer, we will recap what a bytecode baseline is, see why --release is the right switch, look at multi-release JARs and run-time detection, and finish with how we would move the baseline forward over time.
1A Quick Recap of Class File Versions
When javac compiles our code, every class file it writes carries a version number in its header. Java 17 writes version 61, Java 21 writes 65, and Java 25 writes 69. A JVM refuses to load a class file with a version newer than its own, and it fails with UnsupportedClassVersionError, which names both numbers.
So the bytecode baseline of a library is simply the class file version we compile to. Because newer JVMs always load older class files, the safe baseline is the oldest Java version among the services that use the library. In our case the services run on 17, 21 and 25, so the baseline is 17. A Java 17 class file runs unchanged on 21 and 25 and still benefits from their faster JIT and garbage collectors.
2Why --release Is Better Than -source and -target
Older build scripts set -source 17 -target 17. That controls the language level and the class file version, but there is a catch: javac still compiles against the class library of the JDK it is running on. If we build with JDK 25, our code can call a method that only exists in 21 or later, and the compiler will not complain.
The --release flag fixes this. It sets the language level and the class file version, and it also compiles against a recorded description of the Java 17 public API that ships inside every newer JDK. So the mistake is caught on the build machine instead of in a service that happens to run Java 17. In Maven we set maven.compiler.release to 17, and in Gradle we set options.release to 17 or use a toolchain. Notice that this lets us build everything with the newest JDK, which has the best compiler and tools, while still producing Java 17 bytecode.
3Using Newer APIs With a Multi-Release JAR
Sometimes the library would really benefit from a newer API, for example virtual threads for an internal executor on Java 21 and later. A multi-release JAR lets us ship two versions of the same class in one artifact. The normal class sits at the root of the JAR, compiled with --release 17. A second copy, compiled with --release 21, sits under META-INF/versions/21/, and the manifest says Multi-Release: true.
A Java 21 or 25 JVM looks in the versioned folder first and uses that copy; a Java 17 JVM ignores the folder and uses the root copy. The rest of the library keeps calling the same class name and never knows which one it got.
- ManifestMulti-Release: true
- META-INF/versions/25/optional copies used only on Java 25 and later
- META-INF/versions/21/copies used on Java 21 and later, compiled with --release 21
- Root classescompiled with --release 17; the only copies Java 17 sees
4 layers, from the top down. Step through them one at a time.
A JVM picks the highest folder that is not newer than itself, and falls back to the root.
The jar tool's --release option places the following files in the versioned folder and checks that the versioned class has the same public API as the root one, which is the rule that keeps a multi-release JAR safe. The cost is real: two source sets, a more complicated build, and tests that must run on each Java version, because a bug can hide in only one copy. So we keep the versioned part to a handful of small classes behind a stable interface.
There is also a quieter catch. Some tools that scan JARs, such as older shading plugins or code coverage agents, ignore the versioned folder, so check that your build and analysis tools understand multi-release JARs before relying on them.
4Detecting Features at Run Time
The other option is to keep one set of classes and decide at run time. The library checks Runtime.version().feature(), or looks up a method reflectively or with a MethodHandle, and picks a code path. This works without any special JAR layout, and it is how many libraries supported old JDKs for years.
But the cost moves into the code. Every newer API must be called through reflection or a method handle, because a direct call would not compile with --release 17. The code is harder to read, errors show up only at run time, and tests still have to run on each version to cover both branches. We use this approach only for one or two small checks, never as the general way to use new APIs.
5Raising the Baseline on a Schedule
A baseline is not forever. Every year we keep 17 as the floor, we pay for it in older APIs, multi-release builds and test matrices. So we publish a support policy, for example: the library supports the current LTS and the previous one, and the baseline moves up six months after each new LTS. With Java 25 released in September 2025, that would mean announcing that the library moves to a Java 21 baseline in a stated month, and giving services on 17 a date to upgrade.
We announce the date early, keep a last release line on the old baseline for security fixes during the overlap, and track which services still run the old version so we can talk to their owners before the change instead of breaking them. When the baseline moves, the multi-release copies for 21 become the main code and the extra source set disappears, which is a real simplification.
6Follow-up questions
What happens if a service on Java 17 pulls in a library compiled with --release 21?
The service fails when it first loads a class from that library, with java.lang.UnsupportedClassVersionError saying the class was compiled by a more recent version of the Java Runtime (class file version 65.0) and this runtime only recognizes versions up to 61.0. It usually happens at startup, but with lazy loading it can happen later on a rarely used path, which is why we also check class file versions in the consuming service's build, for example with the Maven Enforcer enforceBytecodeVersion rule from extra-enforcer-rules.
Can we use newer language features, such as pattern matching for switch, in a library with a Java 17 baseline?
No. --release 17 also fixes the language level, so javac rejects syntax that came after 17. Pattern matching for switch and record patterns became final in Java 21, so the library cannot use them in its root classes. It can use them inside the versioned classes of a multi-release JAR, because those are compiled with --release 21, but they must keep the same public API.
Should we build the library with JDK 17 or with the newest JDK?
With the newest JDK we support, using --release 17. The newer javac has bug fixes and better warnings, and --release guarantees both the bytecode version and the API check. We still run the test suite on Java 17, 21 and 25 in CI, because compiling for 17 does not prove the behaviour is the same on every runtime.
7Conclusion
In this answer, we saw that the bytecode baseline is the class file version we compile to, and that the right choice for libraries is the oldest supported Java version, set with --release so javac also checks API usage. We looked at multi-release JARs for using newer APIs where they exist, run-time detection as a narrower alternative, and a published schedule for raising the baseline as services upgrade.
Open with
I would set the baseline to the oldest Java version any consumer runs, so Java 17 here, because a Java 17 class file runs on 21 and 25 but a Java 21 class file fails on 17 with UnsupportedClassVersionError.
Then, if they want more
I would compile with --release 17 rather than -source and -target. The difference matters: -source and -target only set the language level and class file version, while --release also compiles against the Java 17 API, so if someone calls List.reversed(), which only exists since 21, the build fails instead of a service on 17 throwing NoSuchMethodError in production.
That also lets us build with the newest JDK and still produce safe Java 17 bytecode. If we really need a newer API, such as virtual threads in an internal executor, I would use a multi-release JAR: the root classes compiled for 17, and a few replacement classes under META-INF/versions/21 compiled for 21 with the same public API.
The JVM picks the right copy automatically. The cost is a second source set and testing on every version, so I keep it small. For one or two checks I might detect the feature at run time with Runtime.version() and reflection, but I would not build a whole library that way.
Finally, I would publish a support policy, for example the current and previous LTS, and move the baseline to 21 on an announced date, keeping a maintenance line on the old baseline for security fixes during the overlap.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 21, Java 25
- 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.
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 "Your shared libraries must support services on Java 17, 21 and 25. How do you decide the...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/495b2038fe/