Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans 11.3 and Java 14 Preview Features

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.

like image 649
Rintoul Avatar asked Dec 04 '25 03:12

Rintoul


2 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.

like image 66
skomisa Avatar answered Dec 05 '25 18:12

skomisa


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>

like image 32
Felix Aballi Avatar answered Dec 05 '25 20:12

Felix Aballi