Sub topic 9
Compiling and running: JDK versions and preview flags
This chapter assumes that you can compile and run basic Java classes and understand a little about import declarations. You should also know that --release selects the Java language level and standard API version used by the compiler.
The import module declaration changed status across several JDK releases. Code that uses it must be compiled with a JDK that supports the feature, and preview releases need special compiler and runtime flags.
Feature status by JDK version
import module was introduced as a preview feature in JDK 23. It remained a preview feature in JDK 24 and became a final feature in JDK 25.
| JDK version | Status | Required flags |
|---|---|---|
| JDK 23 | First preview | `--enable-preview --release 23` |
| JDK 24 | Second preview | `--enable-preview --release 24` |
| JDK 25 and later | Final feature | No preview flag |
A preview feature is an experimental language or platform feature included in a JDK before its design is final. The compiler requires you to opt in. The JVM also requires an opt-in when it runs classes compiled with preview features.
Compiling with JDK 25
With JDK 25 or a later JDK, compile a source file normally. The following program uses a module import to make types from java.base available by simple name:
import module java.base;
public class ModuleImportDemo {
public static void main(String[] args) {
List<String> names = List.of("Ada", "Grace", "Alan");
String joined = names.stream()
.sorted()
.collect(Collectors.joining(", "));
System.out.println(joined);
}
}Save the file as ModuleImportDemo.java. Then run:
javac --release 25 ModuleImportDemo.java
java ModuleImportDemoThe output is:
Ada, Alan, GraceThe --release 25 option asks javac to compile for the Java 25 language and API level. If you are using JDK 26 or a later JDK, you can select the release supported by your project instead. The compiler and runtime should be compatible with the selected release.
Compiling with a preview JDK
JDK 23 and JDK 24 require --enable-preview during compilation. You must also provide a matching --release value.
For JDK 23, use:
javac --release 23 --enable-preview ModuleImportDemo.java
java --enable-preview ModuleImportDemoFor JDK 24, use:
javac --release 24 --enable-preview ModuleImportDemo.java
java --enable-preview ModuleImportDemoThe two options have different jobs:
--release 23or--release 24selects the language and API version.--enable-previewpermits preview syntax during compilation.java --enable-previewpermits the JVM to run a class file that uses preview features.
The release number must match the JDK used to compile the source. For example, do not compile with --release 23 and expect a JDK 22 compiler to understand the feature. JDK 22 predates the feature.
The compile and run sequence
The required steps form a simple pipeline:
If compilation succeeds but execution fails with a message about a preview feature, check the run command first. A preview class requires java --enable-preview, even when compilation completed successfully.
If compilation reports that import module is not recognized, check the compiler version and the release option. You may be using a JDK older than 23, or you may have omitted --enable-preview on JDK 23 or 24.
Checking the tools in use
The java and javac commands can point to different JDK installations. Check both:
java --version
javac --versionThey should refer to compatible JDK versions. A common mistake is to compile with a newer javac and run with an older java. The source may compile successfully but fail during execution because the older runtime cannot understand the generated class file.
Exercise
You are using JDK 24 and want to compile and run a class containing import module java.base;. Which commands should you use?
Show answer
Compile with javac --release 24 --enable-preview FileName.java, then run with java --enable-preview FileName.