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

5

16

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.

Botello answered 2/5, 2022 at 7:50 Comment(8)
baeldung.com/java-preview-features – Rascal
@Rascal 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. – Botello
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. – Rascal
Try adding --enable-preview to Preferences | Build, Execution, Deployment | Compiler | Java Compiler in the Additional command line parameters text field. – Maniac
@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. – Botello
@BasilBourque sir.. 1 question which is not related to your question. Is it ok to use to preview-features in production? – Barretter
@Barretter The answer to your query should be self-evident. If not, read the documentation: JEP 12: Preview Features. – Botello
I am a bit late but I had the same problem. I found this page: hanno.codes/2023/07/16/… In the last part he goes to Settings -> Build, Execution, Deployment -> Java Compiler and adds enable-preview option to the compiler options. That solved my issue – Jarnagin
B
18

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

Bradshaw answered 4/5, 2022 at 18:4 Comment(2)
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: imgur.com/a/D0Hqq8e And my run config: imgur.com/a/6YTCkm5 Which meant I could write code that used virtual threads: imgur.com/a/GwxYrZF – Underproof
For gradle users, in addition to the points mentioned above I also had to add the code mentioned in https://mcmap.net/q/112648/-how-to-set-the-use-enable-preview-compile-and-run-flags-from-gradle – Biparietal
A
9

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>
Ascidium answered 11/11, 2022 at 17:40 Comment(0)
F
5

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.

Fusible answered 19/6, 2023 at 17:23 Comment(0)
M
2

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>
Mcbee answered 21/2 at 9:5 Comment(0)
M
1

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']
    }
    
Myrmeco answered 16/12, 2023 at 5:0 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.