Netbeans 11.3 and Java 14 Preview Features

Viewed 3534

I am using Java 14 as the default Java platform for Netbeans 11.3 (netbeans_jdkhome is set to my Java 14 JDK) and trying to use a preview feature in a simple Java application. I set the source level to 14 and set --enable-preview as a compiler argument. The code compiles without errors.

However, when I try to run it within Netbeans, it complains that the major version of the .class files is 57 while the runtime only plays well with 58 files and preview features. Here's the error:

java.lang.UnsupportedClassVersionError: javaapplicationtest14/JavaApplicationTest14 (class file version 57.65535) was compiled with preview features that are unsupported. This version of the Java Runtime only recognizes preview features for class file version 58.65535

I checked the major version of the .class files and they are indeed 57. Any ideas why my project won't compile into Java 14 level? I am using an Ant build.

3 Answers

As well as setting --enable-preview as a compiler option, it should also be set as a VM Option when running the code:

enter image description here

However, that doesn't fix the problem, and unfortunately this looks like a NetBeans 11.3 bug. I reproduced your problem with a Java with Ant project, and created Bug Report NETBEANS-4049 UnsupportedClassVersionError when running JDK14 code with --enable-preview.

There are a couple of workarounds if you need to use preview features with JDK 14 in NetBeans:

  • Run your application from the command line (with --enable-preview as an option) instead of within NetBeans. The same code which fails with the UnsupportedClassVersionError in NetBeans runs fine in that environment, which strongly suggests that NetBeans is ignoring the --enable-preview run time option.
  • Create a Java with Maven project instead of a Java with Ant project. You can then run your code which uses preview features within NetBeans.

Update your question with more details if you still have problems.

Netbeans Project Configuration (Java 14)

  • Java 14
  • Netbeans >= 11 (Current: 12.0 LTS)

Optional:

Can use sdkman or set default java path:

  /opt/<jdk-install-dir>
C:\Program Files\<jdk-install-dir>

Project 'Run' Configuration

Project Run Configuration

Java Platform

Build / Compile

pom.xml

Notes

  • Check maven.compiler.source / maven.compiler.target
  • Check build->plugins->plugin->...-> compilerArgs -> arg
<?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>com.mycompany</groupId>
    <artifactId>Demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <compilerArgs>
                        <arg>--enable-preview</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

nb-javac should not be installed (it appears in the plugins).

If it is installed in 11.3, it seems to create classfiles with version 57 not 58, which the runtime then objects to, as above.

Related