Enabling preview features in Java 25

  • Last Updated: November 26, 2025
  • By: javahandson
  • Series
img

Enabling preview features in Java 25

To try the newest language enhancements coming to the Java platform—such as primitive patterns (JEP 507), Scoped Values, and Structured Concurrency—we must explicitly turn on preview mode. Enabling Preview Features in Java 25 gives developers early access to these powerful capabilities, even though they are not yet final. This allows us to experiment, learn, and provide feedback before these features become part of the permanent language specification, while ensuring we opt in deliberately and avoid accidental reliance on evolving syntax or behavior.

 

Because preview features are disabled by default, Java requires them to be enabled at both compile time and runtime. Whether you are running simple javac commands, working with Maven or Gradle, or testing new JEPs for your blog or project, knowing how to enable preview mode in Java 25 is essential. This guide walks through the exact steps so you can start exploring all the exciting preview capabilities safely and effectively.

Enable preview features while compiling

To compile code that contains primitive patterns, we use:

javac --enable-preview --release 25 Main.java

Here:

–enable-preview turns on preview features

–release 25 ensures we compile against the Java 25 feature set

Without these flags, the compiler will reject code that uses primitive patterns.

Enable preview features while running

After compiling, we also need to enable preview features while running the program:

java --enable-preview Main

If we forget this step, the JVM will refuse to run the class and will warn us that the code depends on preview features.

Enabling preview in IntelliJ IDEA

If we prefer using an IDE, IntelliJ makes this easy:

1. Open Project Structure → Project
2. Set Project SDK to JDK 25
3. Set Language Level to 25 (Preview)
4. Go to Run/Debug Configurations
5. Add –enable-preview under VM Options

Now the IDE will compile and run code using primitive patterns without errors.

IDEs Will Continue Warning us because this is a preview feature:

  • IDEs may show small warnings
  • APIs may change in future versions
  • Code may need minor adjustments when the feature becomes final

This is expected for all preview features in Java.

Enabling preview in Maven

If our project uses Maven, we add these flags inside the compiler plugin:

<build>
    <plugins>

        <!-- Compiler Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.12.1</version>
            <configuration>
                <release>25</release>
                <compilerArgs>
                    <arg>--enable-preview</arg>
                </compilerArgs>
            </configuration>
        </plugin>

        <!-- Test Plugin (Surefire) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.5</version>
            <configuration>
                <argLine>--enable-preview</argLine>
            </configuration>
        </plugin>

    </plugins>
</build>

And for running the app using Maven:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <arguments>
            <argument>--enable-preview</argument>
            <argument>-classpath</argument>
            <argument>%classpath</argument>
            <argument>com.javahandson.Main</argument>
        </arguments>
    </configuration>
</plugin>

This exec-maven-plugin is useful only if we want to run our Java application using Maven like this:

mvn exec:java

But enabling preview features in Maven does NOT require this plugin.

What is required is:

Compiler plugin → for compiling preview features
Surefire plugin → for running tests using preview features

Enable preview features in Gradle

Build file – build.gradle

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
    }
}

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs += ["--enable-preview"]
}

tasks.withType(Test).configureEach {
    jvmArgs += ["--enable-preview"]
}

tasks.withType(JavaExec).configureEach {
    jvmArgs += ["--enable-preview"]
}

1. The above Gradle script applies the preview flag to all JavaCompile tasks.

2. Ensures JVM (tests, run tasks, exec tasks) is run with preview enabled

3. Uses toolchain → avoids local Java mismatch issues.

This is the same structure that Gradle recommends for Java 21 preview and remains valid for Java 25.

Verify whether preview is enabled

Run:

java --version

If preview is used, the JVM logs the following warning when running our application:

Warning: Preview features are enabled for this release.

Running a preview feature

record Point(int x, int y) {}

public class Demo {
    public static void main(String[] args) {
        Object obj = new Point(5, 10);

        if (obj instanceof Point(int a, int b)) {
            System.out.println(a + b);
        }
    }
}
Compile + Run

javac --enable-preview --release 25 Demo.java
java --enable-preview Demo

Why does Java keep preview features Opt-In?

Preview mode gives the Java community:

  • time to test new features
  • time to give feedback
  • a chance to refine syntax and behavior

This approach ensures long-term stability and helps Java avoid breaking changes.

Conclusion

Enabling preview features in Java 25 gives us early access to some of the most exciting features coming to the language: primitive patterns, Scoped Values, Structured Concurrency, and more. These enhancements push Java forward in performance, readability, and developer productivity, and using them early helps us build familiarity before they become standard. By explicitly enabling preview features, we can explore cutting-edge capabilities while keeping our applications safe from accidental breakage.

Whether you are compiling with javac, running your application, or configuring Maven or Gradle, the process to Enable Preview Features in Java 25 is simple and consistent. With just a few flags, you unlock a powerful learning environment where you can experiment, practice, and stay ahead of the evolution of Java. Preview features are Java’s way of inviting developers into the future to use them wisely, explore confidently, and be ready for what’s coming next.

Leave a Comment