Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

How to Enable Java Preview Features and Run Code from IntelliJ IDEA

  • May 08, 2022
  • 16009 Unique Views
  • 2 min read

JEP 425: Virtual Threads (Preview) has been proposed recently. It has been a long-awaited feature in Java.  I wanted to give it a try. So I download the early release of JDK which has the project loom in it. However, it is under preview. 

The following snippet was pretty much my first program written for testing virtual threads.

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread.startVirtualThread(() -> {
            System.out.println("Hello from virtual thread");
        }).join();
    }
}

It is so simple that I could just run it in the command line using the source code launcher :

java --enable-preview --release 19 Main.java

However, it needed a bit of yak shaving. I needed to download the JDK, extract the tarball, set the java home, etc. I manage multiple JDKs using SDKMAN; it doesn't have it since it's still in early access release. So I had to let SDKMAN know it manually.

Then I figured, maybe, perhaps an IDE could help me here. So I opened my favourite IDE, which happens to be IntelliJ IDEA. I created a project and set up the JDK using the following window-

Then when I tried to run, it didn’t allow me to run the code since the virtual thread was still in preview. Here are the steps I had to go through in IntelliJ IDEA.

First, you need to go preference, and then Build, Execution, Deployment and then Select Java Compiler.

At the bottom, there is a box named the additional command line parameter. Add the following line there-

--enable-preview

And then go to the run configuration. Select the modify options and Mark the Add VM options.

You need to add --enable-preview there as well.

That’s it.

Now you can run the project loom from IntelliJ IDEA.

Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Comments (6)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Suresh avatar

Suresh

3 years ago

>I manage multiple JDKs using SDKMAN; it doesn't have it since it's still in early access release. SDKMan has been providing loom EA releases for s very long time. All you have to do is `sdk i java 19.ea.5.lm-open`

-5

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

A N M Bazlur Rahman avatar

A N M Bazlur Rahman

3 years ago

So this has happened with SDKMAN in mac with me, when I tried loom EA. I don't know if this is because of my machine or something with SDKMAN <code> W $sdk d java 19.ea.21-open We periodically need to update the local cache. Please run: $ sdk update Default java version set to 19.ea.21-open \W $java --version The operation couldn’t be completed. Unable to locate a Java Runtime. Please visit http://www.java.com for information on installing Java. \W $</code>

-1

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Java SE 13 新增特性 | 呱唧呱唧网

2 years ago

[…] 注:如果你用Java SE 12运行上述代码,需要指定--enable-preview参数,如果使用 Intellij IDEA ,参考How to Enable Java Preview Features and Run Code from IntelliJ IDEA […]

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Deepu K Sasidharan avatar

Deepu K Sasidharan

2 years ago

With the latest build (19.ea.36-open) you also need to set `--source 19` in the Java compiler parameters for this to work. Setting `--release 19` doesn't work

-3

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Java 17 (LTS) İle Gelen Yenilikler - Kıvırcık Kafa

2 years ago

[…] olduğundan, etkinleştirmek için –enable-preview seçeneğini kullanmamız gerekiyor. Nasıl yapıldığına buradan […]

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

423摘要 - Java SE 13 新增特性

2 years ago

[…] 注:如果你用Java SE 12运行上述代码,需要指定--enable-preview参数,如果使用 Intellij IDEA ,参考How to Enable Java Preview Features and Run Code from IntelliJ IDEA […]

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard