Sub topic 3
Which Java versions include scoped values and how to enable them
This chapter assumes you can compile and run Java programs from the command line and know what a JDK release and an LTS release are. It also assumes you have read the earlier chapters on why scoped values exist. Here you will learn which release to use and which flags each release needs.
Scoped values by release
Scoped values did not arrive all at once. They went through an incubator phase and several preview rounds before becoming a final feature. The table lists each stage. JEP means JDK Enhancement Proposal, the document that specifies a JDK feature.
| Java release | JEP | Status | Package | Flags needed |
|---|---|---|---|---|
| 20 | 429 | Incubator | `jdk.incubator.concurrent` | `--add-modules jdk.incubator.concurrent` |
| 21 (LTS) | 446 | Preview | `java.lang` | `--enable-preview` |
| 22 | 464 | Second preview | `java.lang` | `--enable-preview` |
| 23 | 481 | Third preview | `java.lang` | `--enable-preview` |
| 24 | 487 | Fourth preview | `java.lang` | `--enable-preview` |
| 25 (LTS) | 506 | Final | `java.lang` | None |
Java 21 and Java 25 are long-term support (LTS) releases. Most teams standardize on an LTS release, so these two are the versions you are most likely to meet.
What each status means
An incubator module is an experimental API shipped in a separate module whose name starts with jdk.incubator. You must add the module explicitly. The API lives in a package that can be renamed or removed later.
A preview feature is fully specified and implemented but not yet permanent. The JDK team ships it so developers can try it and give feedback. The API can still change between releases. Preview APIs are disabled by default, and you opt in with --enable-preview.
A final feature is part of the platform. It needs no flags and follows the usual compatibility rules.
Which release should you use?
The code in this tutorial uses java.lang.ScopedValue. That class exists in Java 21 and later.
- Java 25 or later: Use this if you can. The API is final, and you need no flags.
- Java 21 through 24: The examples work, but you must pass
--enable-previewto the compiler and to the JVM. Between these releases, small API details changed, so check the Javadoc for your exact version when something does not compile. - Java 20: The class is
jdk.incubator.concurrent.ScopedValue. The import and the flags differ from everything else in this tutorial. Java 20 is no longer supported, so avoid it unless you are maintaining old code. - Java 19 and earlier: Scoped values are not available. Use
ThreadLocal.
Checking your Java version
Run java -version and read the first line. You can also check from code, which is useful in a build script or a bug report.
public class VersionCheck {
public static void main(String[] args) {
System.out.println("Java feature release: " + Runtime.version().feature());
System.out.println("Full version: " + Runtime.version());
}
}Run it with java VersionCheck.java. On a Java 25 JDK it prints Java feature release: 25. If the number is below 21, the examples in this tutorial will not compile.
Enabling preview on Java 21 through 24
Preview features need the flag in two places: when you compile and when you run. Add --release with the same number as your JDK, because preview features are tied to one specific release.
public class Hello {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice")
.run(() -> System.out.println("Hello, " + USER.get()));
}
}Save this as Hello.java and compile and run it on a Java 21 JDK:
javac --release 21 --enable-preview Hello.java
java --enable-preview HelloReplace 21 with your JDK's number. The compiler prints a note that the file uses preview features of Java 21. That note is normal.
For a single source file you can skip the separate compile step:
java --enable-preview --source 21 Hello.javaOn Java 25 or later, drop all the flags:
java Hello.javaCommon errors when the flag is missing
The two missing-flag cases produce different errors, and each points at a different step.
| Message | Cause | Fix |
|---|---|---|
| `ScopedValue is a preview API and is disabled by default. (use --enable-preview to enable preview APIs)` | You compiled without the flag. | Add `--enable-preview` and `--release N` to `javac`. |
| `java.lang.UnsupportedClassVersionError: Preview features are not enabled for Hello (class file version 65.65535). Try running with '--enable-preview'` | You compiled with the flag but ran without it. | Add `--enable-preview` to `java`. |
| `class file version 65.65535 ... this version of the Java Runtime only recognizes class file versions up to 66.0` | You compiled with preview on Java 21 and ran on a different JDK. | Run on the same feature release you compiled with. |
The class file version 65.65535 tells you the class was compiled with preview features. The minor version 65535 is the marker. Java refuses to run such classes on any other release, because the preview API may have changed.
Configuring a build tool
The flag has to reach the compiler and every JVM that runs your code, including the test JVM. Missing the test JVM is the most common build failure.
For Maven on Java 21:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>21</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>For Gradle (Kotlin DSL):
tasks.withType<JavaCompile> {
options.compilerArgs.add("--enable-preview")
}
tasks.withType<Test> {
jvmArgs("--enable-preview")
}
tasks.withType<JavaExec> {
jvmArgs("--enable-preview")
}Once you move to Java 25, remove these settings. Leaving --enable-preview on is harmless on its own. It does mark your classes as preview-dependent, though, and that can stop them from loading on a later JDK.
Java 20 incubator usage
If you must work on Java 20, change the import and add the module:
import jdk.incubator.concurrent.ScopedValue;
public class Hello20 {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice")
.run(() -> System.out.println("Hello, " + USER.get()));
}
}javac --add-modules jdk.incubator.concurrent Hello20.java
java --add-modules jdk.incubator.concurrent Hello20The method have seen so far (newInstance, where, run, get) were the same then. Code written for the incubator version usually needs only an import change to move to Java 21.
What is still preview in Java 25
Scoped values are final in Java 25, but a related feature is not. Structured concurrency (StructuredTaskScope) is still a preview API in Java 25. A later chapter shows how forked subtasks inherit scoped value bindings, and that part still needs --enable-preview on Java 25. You can use scoped values on their own without any flag.
Decision checklist
- Run
java -versionand note the feature release. - If it is 25 or higher, write and run code with no flags.
- If it is 21 to 24, add
--enable-preview(and--release Nat compile time) everywhere code compiles or runs, including tests. - If it is 20, import from
jdk.incubator.concurrentand add the module. - If it is lower than 20, stay with
ThreadLocalor upgrade.
Exercise
You compile a class that uses ScopedValue on a Java 22 JDK with javac --release 22 --enable-preview App.java. Running java App then fails with an UnsupportedClassVersionError that mentions preview features. What is wrong, and how do you fix it?
Show answer
The class was compiled with preview features, so its class file is marked as preview, but the JVM was started without --enable-preview. Run it with java --enable-preview App, on the same Java 22 JDK that compiled it.
Exercise
A teammate on Java 24 sees "ScopedValue is a preview API and is disabled by default" in an IDE build, although the command line works with --enable-preview. Name the likely cause and one fix.
Show answer
The IDE or build tool compiles without passing --enable-preview to javac. Add the flag to the compiler arguments in the build configuration (for example compilerArgs in Maven or options.compilerArgs in Gradle) and to the test or run JVM arguments. Alternatively, upgrade to Java 25, where scoped values are final and need no flag.