Sub topic 9
Compiling and Running: JDK Versions and Preview Flags
This chapter assumes you can compile and run Java from the command line and know what --release does. It also assumes you have read the earlier chapters on the import module syntax. The focus here is the toolchain. You will see which JDK versions accept import module, which flags each one needs, and which errors appear when the version and flags don't match.
Three phases of the feature
import module was introduced as a preview feature, refined in a second preview, and finalized.
| JDK | Status | JEP | Flags needed to use `import module` |
|---|---|---|---|
| 22 and earlier | Not available | none | The syntax does not parse |
| 23 | First preview | 476 | `--enable-preview` and `--release 23` |
| 24 | Second preview | 494 | `--enable-preview` and `--release 24` |
| 25 and later | Final (permanent) | 511 | None |
JDK 25 is a long-term support release, so it is the first LTS release where the feature is permanent. If your team standardizes on LTS releases, you can use import module without preview flags from 25 on.
The second preview in JDK 24 changed two rules. A module may now declare requires transitive java.base. A type-import-on-demand declaration such as import java.util.*; can now shadow a module import. You do not need to memorize these. Just know that code written for JDK 23 can behave differently on JDK 24 in corner cases, which is one reason preview features are tied to a single release.
What "preview" means for the toolchain
A preview feature is fully specified and implemented, but it is not yet permanent. The JDK ships it disabled and makes you opt in. Three rules follow from this:
- The compiler rejects preview syntax unless you pass
--enable-preview. --enable-previewmust be accompanied by--release N(or-source N), where N equals the version of thejavacyou are running. A JDK 25javaccannot compile preview code for release 23.- Class files that depend on a preview feature are marked, and the JVM only loads them if you pass
--enable-previewand the JVM is the same feature release that compiled them.
The marking in rule 3 is a minor version of 65535 in the class file header. You can see it with javap.
A program to test your setup
This small program imports java.base and prints the Java version it is running on. It is a convenient smoke test because it fails to compile on JDK 22 and earlier, and it needs flags on 23 and 24.
import module java.base;
public class ModuleImportDemo {
public static void main(String[] args) {
List<String> names = List.of("Ada", "Grace", "Alan");
Map<String, Integer> lengths = new HashMap<>();
for (String name : names) {
lengths.put(name, name.length());
}
String joined = names.stream()
.sorted()
.collect(Collectors.joining(", "));
System.out.println(joined);
System.out.println(lengths.get("Grace"));
System.out.println("Running on Java " + Runtime.version().feature());
}
}List, Map and HashMap come from java.util, and Collectors comes from java.util.stream. Both packages are exported by java.base, so one declaration covers them all.
Compiling and running on each JDK
JDK 25 and later
No flags are needed beyond the usual ones.
javac --release 25 ModuleImportDemo.java
java ModuleImportDemoThe output is:
Alan, Ada, Grace
5
Running on Java 25Wait, the sorted order matters. sorted() uses natural string order, so the first line is Ada, Alan, Grace. The correct output is therefore:
Ada, Alan, Grace
5
Running on Java 25JDK 23 and JDK 24
Pass --enable-preview to both tools, and match --release to the JDK you are using.
# JDK 23
javac --release 23 --enable-preview ModuleImportDemo.java
java --enable-preview ModuleImportDemo
# JDK 24
javac --release 24 --enable-preview ModuleImportDemo.java
java --enable-preview ModuleImportDemojavac prints a note such as uses preview features of Java SE 23. This is expected. To see which lines triggered it, add -Xlint:preview.
Source-launcher mode
You can skip the separate compile step by running the source file directly. On a preview release you must supply --source along with --enable-preview.
# JDK 23
java --enable-preview --source 23 ModuleImportDemo.java
# JDK 25 and later
java ModuleImportDemo.javaIn this mode the compiled classes live only in memory, so no marked class files are left on disk.
JDK 22 and earlier
There is no flag that helps. The compiler does not recognize import module, and the declaration fails with a syntax error. The only fix is to use a newer JDK, or to replace the module import with explicit imports.
Reading the error messages
Most failures come from a mismatch between the JDK, the flags and the class files. The exact wording varies by release, but the causes are consistent.
| Symptom | Cause | Fix |
|---|---|---|
| Compile error saying `import module` is a preview feature and is disabled by default | JDK 23 or 24, and no `--enable-preview` | Add `--enable-preview --release N` |
| Compile error that `--enable-preview` must be used with `--release` or `-source` | The flag is present but the release is missing | Add `--release N` with N equal to the `javac` version |
| Compile error that the release version is not valid for preview | `--release` differs from the running `javac`, for example `--release 23` on JDK 25 | Use the matching JDK, or move to 25 and drop the flag |
| Syntax error at `import module` | JDK 22 or earlier | Upgrade the JDK |
| `UnsupportedClassVersionError` mentioning preview features at run time | Class compiled with `--enable-preview` but run without it | Add `--enable-preview` to `java` |
| `UnsupportedClassVersionError` mentioning a class file version | Class compiled on 23 and run on 24 (or any other different release) | Recompile on the JVM's own release |
The last row is the one that surprises people. A class compiled against a preview release only runs on that same release, even when you pass --enable-preview. Preview features may change between releases, so the JVM refuses to guess.
Checking what you compiled
The javap tool reports the class file version. Preview-dependent classes have minor version 65535.
javap -v ModuleImportDemo.class | grep -E "major|minor"On JDK 23 with --enable-preview the output is:
minor version: 65535
major version: 67On JDK 25 without preview flags, you get minor version: 0 and major version: 69. The major versions are 67 for JDK 23, 68 for JDK 24 and 69 for JDK 25. If you ever need to confirm that a library you ship does not require --enable-preview from its users, this check is the quickest one.
Build tools
Build tools need the flag in two places: the compile step and the test or run step. Forgetting the second is a common cause of UnsupportedClassVersionError in CI.
With Maven on JDK 23 or 24, set the compiler release and arguments, and pass the flag to the tests:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>23</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>Once you move to JDK 25, delete both the --enable-preview entries and change <release> to 25. Leaving the flag in does not break anything, but it keeps marking your class files as preview-dependent if any other preview feature is in use, and it hides the fact that you no longer need it.
Choosing a strategy
The right approach depends on how long the code will live and who runs it.
- Libraries and shared code: do not use preview features. A library compiled with
--enable-previewforces every consumer to run with the flag on the same JDK release. Wait for JDK 25 or write explicit imports. - Scripts, experiments and learning projects: preview is fine on 23 or 24, particularly with source-launcher mode, because nothing is distributed.
- Applications you fully control: you can use preview in production if you pin the JDK exactly. Plan the move to 25, since each preview release needs a recompile.
Behavior that does not depend on the version
Once the code compiles, the semantics of module imports are the same across the preview releases and the final one, apart from the two JDK 24 refinements noted earlier. The java.sql example from the previous chapter, where Date is ambiguous between java.util.Date and java.sql.Date, produces the same kind of compile error on JDK 23, 24 and 25. The fix with a single-type import also works on all three.
import module java.base;
import module java.sql;
import java.util.Date;
public class ResolvedDate {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now.getTime() > 0);
}
}Compile it on JDK 25 with javac --release 25 ResolvedDate.java. It prints true. On 23 or 24, add --enable-preview and the matching --release.
One case does differ. If you import a module and also use an on-demand import of a package from the same module, JDK 23 treats the combination more strictly than 24 and later, where the on-demand import is allowed to shadow the module import. Code that compiles on 24 or 25 might need a single-type import to compile on 23.
Exercises
Exercise
You compile a class that uses import module java.base; on JDK 23 with javac --release 23 --enable-preview Demo.java. Running java Demo fails with an UnsupportedClassVersionError about preview features. What is missing, and what minor version would javap -v Demo.class report?
Show answer
The run command needs --enable-preview, so use java --enable-preview Demo. The class file carries minor version 65535, which marks it as dependent on preview features, and the JVM refuses to load such a class unless preview is enabled.
Exercise
A colleague on JDK 25 runs javac --release 23 --enable-preview Demo.java and gets an error. Why does it fail, and what should they do instead?
Show answer
Preview features can only be enabled for the release equal to the running javac. A JDK 25 compiler cannot produce preview output for release 23. Since import module is final in JDK 25, they should compile with javac --release 25 Demo.java and no preview flag.
Exercise
A library you publish uses import module java.base; and is built on JDK 24 with --enable-preview. Name two problems this causes for the library's users, and one way to avoid them.
Show answer
Users must run with --enable-preview, and they must run on JDK 24 exactly, because the class files are marked as preview and are only loadable on that release. To avoid this, replace the module import with explicit imports, or build the library on JDK 25 or later where the feature is final and needs no flag.