Issues in running virtual threads program on intellij with java19

941 views Asked by At

I am trying to run below simple program with virtual threads on my intellij with java19 version selected.

Code

public class VTSimple {
    public static void main(String[] args) {
        Runnable runnable = () -> System.out.println("Inside Runnable");
        Thread.startVirtualThread(runnable);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.java19</groupId>
    <artifactId>java19-explore</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
    </properties>

</project>

Project Settings

SDK - 19

Language Level - X Experimental Features as last version shown was 17(preview) in Language Level dropdown Also Tried SDK Default Option as well.

Error

When I ran the program it gave me below error

java: invalid source release 18 with --enable-preview
  (preview language features are only supported for release 19)

Few Trials

I tried to add --enable-preview in VM Options of this small program and as well as compiler settings in preferences but it didn't work.

Edit 1

intellij compiler preferences

Setup Details :

Mac OS Air M1 : 12.1 Monterey

Intellij Version : IntelliJ IDEA 2021.3.3 (Community Edition) Build #IC-213.7172.25, built on March 15, 2022

Java Version : openjdk 19.0.1 2022-10-18

OpenJDK Runtime Environment (build 19.0.1+10-21)

OpenJDK 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

Edit 2

Updated Intellij to version IntelliJ IDEA 2022.2.3 (Community Edition) and java version 19 was showing in language level. But still there is an error

java: ofVirtual() is a preview API and is disabled by default.
  (use --enable-preview to enable preview APIs)

Note : I have already passed --enable-preview in VM Options of program and Compiler settings in preferences.

2

There are 2 answers

1
Bas Leijdekkers On

You need to use IntelliJ IDEA 2022.2 or newer for Java 19 support.

0
Andrei Drynov On

--enable-preview should be in VM options, NOT program arguments

enter image description here

You still need to have those settings in Java Compiler

enter image description here

And the project settings:

enter image description here

Also, you can run from the command line

java --enable-preview --source 19 Main.java

Sample code:

enter image description here

with the following results:

enter image description here