-1

To use records with JDK 15, I used to compile with arguments --enable-preview --release 15 passed to javac on my Ant build. Everything compiled and run fine.

When using JDK 16, if I compile with the same arguments, I get

error: invalid source release 15 with --enable-preview (preview language features are only supported for release 16)

Records are a standard feature of JDK 16, so I expected that I should compile without those arguments. However, If I remove them, I get

Foo.class uses preview features of Java SE 15. (use --enable-preview to allow loading of classfiles which contain preview features)

And Ant reports a compilation failure:

Compile failed; see the compiler error output for details. at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1425)

So, its a compilation failure with the flags and without them. What's wrong?

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
ARX
  • 1,040
  • 2
  • 14
  • 20
  • 2
    "error: invalid source release 15 with --enable-preview (preview language features are only supported for release 16)" Not sure about ant but you are trying to compile this class with Java 16 compiler along with `--enable-preview --release 15`. You can do this directly without `--enable-preview` when using Java 16. Those parameters are required only when using Java 15. However, if you would like to use Java 16 preview features then do `javac --enable-preview --release 16 ...` – Aniket Sahrawat Apr 19 '21 at 15:30

1 Answers1

4

error: invalid source release 15 with --enable-preview (preview language features are only supported for release 16)

Since you are using JDK-16, you have to use 16 as the value of --release (usually for any feature which is available in JDK-16 as a preview feature). Thus, the following will work for you:

javac --enable-preview --release 16 MyClass.java

However, record is a standard feature in Java-16 and therefore you do not need to compile your code with --enable-preview parameter. You can compile your code without using this parameter i.e.

javac MyClass.java

Foo.class uses preview features of Java SE 15. (use --enable-preview to allow loading of classfiles which contain preview features)

This is not a compile-time error. You get it when trying to run a .java class compiled with --enable-preview parameter, directly. You can run it as

java --enable-preview MyClass

Compile failed; see the compiler error output for details. at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1425)

You need to update your JAVA_HOME setting to point to JDK-16 in order for ant to use JDK-16.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 4
    By the way, Java 16 offers the [*sealed classes*](https://openjdk.java.net/jeps/397) feature in its second preview. So if you want to try that feature, you’ll need to enable preview-mode as discussed in this correct Answer, but for Java 16 rather than Java 15. – Basil Bourque Apr 19 '21 at 16:24
  • But why then Ant says "Compile failed" and doesn't say so when compiling with JDK 15? – ARX Apr 19 '21 at 20:06
  • 1
    @ARX - You need to update your `JAVA_HOME` setting to JDK-16. I missed mentioning it in my answer earlier. I've updated the answer to incorporate it. – Arvind Kumar Avinash Apr 19 '21 at 20:50