Skip to content

Enable Preview Feature in Idea With Gradle 7.6

When I am trying to use the Java virtual thread in Idea with Gradle 7.6, I got error,

Bash
/java-examples/src/main/java/com.veda/app/virtualthread/VirtualTheadExample.java:5: error: ofVirtual() is a preview API and is disabled by default.
        System.out.println(Thread.ofVirtual().factory());
                                 ^
  (use --enable-preview to enable preview APIs)

We need some configurations to enable the preview feature for Idea with Gradle 7.6.

  1. Right click the project and open module setting

    Then in the project -> Language Level, enable the language preview feature.

  2. In the build.gradle file, add the following content

    Groovy
    tasks.withType(JavaCompile) {
        options.compilerArgs.add("--enable-preview")
    }
    
    tasks.withType(Test) {
        jvmArgs += "--enable-preview"
    }
    
    tasks.withType(JavaExec) {
        jvmArgs += "--enable-preview"
    }
    

Now, it should be ok now.