Enable "preview" features in an early-access version of Java in IntelliJ

16.5k views Asked by At

Unfortunately, even the latest early-access versions of IntelliJ often do not yet support early-access versions of Java.

For example, I am trying to use Intellij 2022.1.1 Preview (Ultimate Edition) with the experimental build of Project Loom based on early-access Java 19. Installing JDK generally works with IntelliJ.

But now I want to use the Loom-specific features. When I invoke a method new in to this experimental Java 19, I get this error from compiler:

java: newVirtualThreadPerTaskExecutor() is a preview API and is disabled by default.

(use --enable-preview to enable preview APIs)

My first thought is to set the Language level fields on the File > Project Structure > Project Settings > Project and … Modules panels. But apparently IntelliJ does not offer any menu items for a (Preview) mode for this early-access Java 19.

Is there some way to make IntelliJ utilize the new preview API?

I know the error message's suggestion of --enable-preview is meant to be a flag applied somewhere. But I don't know where.

5

There are 5 answers

0
Ninja Master On

Gradle based Java Compiler Argument, In Intellij IDE

Follow these 2 steps:

  1. Adding VM Options in Run configuration. This VM feature can be found, when you click on Modify Options (can be seen in the screen shot)

enter image description here

  1. In gradle based Java projects we can enable Java features by modifying the "build.gradle" file like below. I'm currently using Java 21.

    compileJava {
       options.compilerArgs += ['--enable-preview']
    }
    
2
Borislav Stoilov On

What I had to do step by step.

Update the IDE to the latest version

Download a JVM with loom

Add the loom JDK to the IDE

Set it both to project and to your build tool

Set enable preview and source to 19 as compiler options to do this, go to prefs -> compiler -> java compiler, uncheck the --release option thing and add the following compiler args for specific project global

--enable-preview --source 19

these are directly passed to javac when it compiles

Set the enable preview on your run configuration and add --enable-preview as a JVM option (if you don't see it click 'Modify options')

You should be good to go, I faced a bug where sometimes Gradle complained that it is not compatible with my JVM, to resolve this I had to switch the Gradle VM to java 17, wait for it to build, and then back to 19

EDIT:

Maven is a better option for experimenting with non-LTS versions. With maven I had zero problems, Gradle has some weird ifs here and there that throw errors if they "Don't support" certain Java-Gradle version combo, even though they use maven beneath

0
exaucae On

I had the same issue while using the Java 19.0.1 EAP for project Panama in InteliJ IDEA Ultimate 2022.2.3:

java: java.lang.foreign.Linker is a preview API and is disabled by default. (use --enable-preview to enable preview APIs)

Without any UI trick, passing the --enable preview argument to maven compiler plugin worked for me:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>19</source>
          <target>19</target>
          <compilerArgs>
            <arg>--enable-preview</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
0
ozeray On

Here is the snapshot for the setting of --enable-preview in IDEA IntelliJ:

enter image description here

Then you will also need to enable preview features for each program you run. On Edit Configuration window, click on Modify Options > AD VM options, and set the flag there:

enter image description here

Now you should be able to run your program with preview features enabled.

0
fascynacja On

I have struggled with same issue although following all the steps from previous answers I was still getting errors while running the project. I am using maven and spring boot. In my case I also needed to update maven config for sping boot with line:

     <jvmArguments>--enable-preview</jvmArguments>

:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
             <configuration>
                   ...
                 <jvmArguments>--enable-preview</jvmArguments>
             </configuration>
    </plugin>