Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8313323

javac -g on a java file which uses unnamed variable leads to ClassFormatError when launching that class

XMLWordPrintable

    • b14

        JEP 443 https://openjdk.org/jeps/443 introduced the "Unnamed Patterns and Variables" preview feature. Consider the following trivial code (also attached as Foo.java):

        public class Foo {
           public static void main(final String[] args) throws Exception {
                final String _ = "foo bar";
                System.out.println("Demo done");
           }
        }

        This uses a unnamed variable.

        Now compile it using:

        javac -g --enable-preview -source 21 Foo.java

        Notice the -g option. The compilation goes through fine. Now try to run that class:

        java --enable-preview Foo

        This fails with the following error

        Error: LinkageError occurred while loading main class Foo
        java.lang.ClassFormatError: Illegal field name "" in class Foo

        Everything, including running the compiled class, works fine if -g is left out during compilation.

        javac version in use:

        javac -version
        javac 21-ea

        java version in use:

        java -version
        openjdk version "21-ea" 2023-09-19
        OpenJDK Runtime Environment (build 21-ea+33-2490)
        OpenJDK 64-Bit Server VM (build 21-ea+33-2490, mixed mode, sharing)

          1. Foo.java
            0.2 kB
            Jaikiran Pai

              jlahoda Jan Lahoda
              jpai Jaikiran Pai
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: