Foojay Today

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

May 08, 2022

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.

Topics:

Related Articles

View All
  • Everyday Shortcuts in IntelliJ IDEA

    Let’s take a look at some of my favourite keyboard shortcuts in IntelliJ IDEA.

    It’s really helpful to use keyboard shortcuts because it speeds you up and keeps you in the flow of coding!

    Read More
    Sep 06, 2021
  • Fix Java Security Issues While Coding in IntelliJ IDEA

    Nowadays, developers are responsible for more than just creating the application. Besides working on features, developers have to focus on their applications’ maintainability, scalability, reliability, and security. Many developers are unsure of where to start with security. In addition, most companies still work with a dedicated security team instead of having security expertise inside the team.

    A lot of developers practically live in their integrated development environment (IDE). A good IDE is like a swiss army knife: it is your go-to tool to do almost everything. Having everything I need to build, run, test, debug, and secure my application, makes a good IDE invaluable for many developers.

    Read More
    Apr 29, 2021
  • IntelliJ IDEA: Beyond The Basics

    IntelliJ IDEA Ultimate is the most powerful IDE for JVM developers in the market by now. It has support for various JVM frameworks, complex refactorings, Integration with VCS, and many more.

    Java developers spend a tremendous amount of time in front of their IDEs. Unfortunately, I’ve noticed that developers don’t take advantage of IDEA’s powerful features.

    In this article, I’ll talk about some tricks that I use in my day to day job. And show you some best practices that can boost your productivity.

    Read More
    Jan 25, 2021

Author(s)

  • A N M Bazlur Rahman

    Bazlur is a Java Champion, a Software Engineer, a JUG leader, a Book Author, and an Editor of InfoQ and Foojay.IO. Bazlur has more than ten years of experience coding ... Learn more

Comments (2)

Your email address will not be published.

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.

Save my name, email, and website in this browser for the next time I comment.

Suresh

>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`

A N M Bazlur Rahman

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

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 $

Subscribe to foojay updates:

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