13

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    https://www.baeldung.com/java-preview-features – Kayaman May 02 '22 at 07:55
  • 2
    @Kayaman Thanks for trying, but that linked page is of no help. The author there describes how to set "Language Level" settings in IntelliJ. But as I mentioned in the Question, there are no appropriate menu items for "Language Level" when using an early-access version of Java. The IntelliJ app does not “know” about the next future version of Java. In my experience, the IntelliJ team can never quite keep up with the next version of Java, despite Java versions coming on schedule every 6 months. – Basil Bourque May 02 '22 at 07:59
  • 1
    Yeah, it just means there's no support for it in IDEA yet. I mean you're using bleeding edge preview features, you can't really blame the lack of support. Especially for something like Loom. I'd expect it needs a lot of work to get proper support for it, and since a preview feature is bound to change, it's a risky feature for such little usage. – Kayaman May 02 '22 at 08:07
  • 1
    Try adding `--enable-preview` to `Preferences | Build, Execution, Deployment | Compiler | Java Compiler` in the `Additional command line parameters` text field. – Bas Leijdekkers May 02 '22 at 10:13
  • @BasLeijdekkers Thanks for the suggestion. I tried that. Now when running a Maven `install`, I get the message "newVirtualThreadPerTaskExecutor() is a preview API and is disabled by default." with `!` icon in a filled red-circle. But I went ahead with an execution — my app ran successfully! So that message seems to be superfluous. – Basil Bourque May 02 '22 at 23:43
  • @BasilBourque sir.. 1 question which is not related to your question. Is it ok to use to preview-features in production? – RamPrakash May 07 '23 at 22:02
  • @RamPrakash The answer to your query should be self-evident. If not, read the documentation: [*JEP 12: Preview Features*](https://openjdk.org/jeps/12). – Basil Bourque May 07 '23 at 23:19

3 Answers3

16

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46
  • 4
    This worked for me using version 2022.1.3 of IntelliJ IDEA Community Edition. The trickiest part for me was figuring out where in the settings to put those compiler options. Here's what my settings looked like when I got it working: https://imgur.com/a/D0Hqq8e And my run config: https://imgur.com/a/6YTCkm5 Which meant I could write code that used virtual threads: https://imgur.com/a/GwxYrZF – Matt Welke Jul 09 '22 at 19:47
  • For gradle users, in addition to the points mentioned above I also had to add the code mentioned in https://stackoverflow.com/a/61849770/846300 – Lukas Hanacek Feb 14 '23 at 09:48
8

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>
exaucae
  • 2,071
  • 1
  • 14
  • 24
2

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.

ozeray
  • 2,134
  • 2
  • 18
  • 13