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

Viewed 739

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.

1 Answers

What I had to do step by step.

Update to 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, got 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 you run configuration 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 resovle this I had to switch the gradle VM to java 17, wati for it to build and them back to 19

EDIT:

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

Related