JDK-8329420 : Java 22 (and 23) launcher calls default constructor although main() is static
  • Type: Bug
  • Component: tools
  • Sub-Component: launcher
  • Affected Version: 22,23
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2024-03-31
  • Updated: 2024-06-13
  • Resolved: 2024-06-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 23
23 b26Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Linux

A DESCRIPTION OF THE PROBLEM :
Java 22 (and 23) launcher calls default constructor although main() is static.

JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview) (Release 22) states:
"In either case, if the chosen method is static then simply invoke it."

REGRESSION : Last worked in version 21.0.2

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
public class HelloWorld {
  public HelloWorld() {
    System.out.println("HelloWorld()");
  }
  public static void main() {
    System.out.println("Hello World!");
  }
}
$ /usr/lib/jvm/java-21-openjdk/bin/javac --release 21 --enable-preview HelloWorld.java && /usr/lib/jvm/java-21-openjdk/bin/java --enable-preview  HelloWorld
Hello World!
$ /usr/lib/jvm/java-22-openjdk/bin/javac --release 22 --enable-preview HelloWorld.java && /usr/lib/jvm/java-22-openjdk/bin/java --enable-preview  HelloWorld
HelloWorld()
Hello World!
$ /usr/lib/jvm/java-23-openjdk/bin/javac --release 23 --enable-preview HelloWorld.java && /usr/lib/jvm/java-23-openjdk/bin/java --enable-preview  HelloWorld
HelloWorld()
Hello World!

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No constructor should be called.
ACTUAL -
For JDK 22 and 23 the default constructor is called.

---------- BEGIN SOURCE ----------
public class HelloWorld {
  public HelloWorld() {
    System.out.println("HelloWorld()");
  }
  public static void main() {
    System.out.println("Hello World!");
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use static void main(String... args) instead.

FREQUENCY : always



Comments
Changeset: cbb6747e Author: Sonia Zaldana Calles <szaldana@openjdk.org> Committer: Jaikiran Pai <jpai@openjdk.org> Date: 2024-06-05 13:12:30 +0000 URL: https://git.openjdk.org/jdk/commit/cbb6747e6b9ce7e2b9e0ffb0a1f9499f7e0e13b0
05-06-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/18786 Date: 2024-04-15 18:25:02 +0000
31-05-2024

Hi [~jlahoda], no problem! This is also fixed with JDK-8329581 so it makes sense.
06-05-2024

Hello Sonia, Given your https://github.com/openjdk/jdk/pull/18786 is fixing this bug, I have assigned it to you. Please let me know/assign back if you disagree. Thanks for working on this!
06-05-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/18753 Date: 2024-04-12 10:16:36 +0000
12-04-2024

Additional Information from submitter: ============================== Additionally to Arch Linux I now also reproduced the bug on Kubuntu 23.10 and Windows 11.
08-04-2024

To reproduce the issue, the reproducer need to be compiled first and run. If I run the source code directly, the problem will be gone. C:\Users\TONGWAN\Documents>"c:\jdk-22.0.1+8"\bin\java --enable-preview --source 22 HelloWorld.java Hello World! C:\Users\TONGWAN\Documents>"c:\jdk-22.0.1+8"\bin\javac --enable-preview --source 22 HelloWorld.java C:\Users\TONGWAN\Documents>"c:\jdk-22.0.1+8"\bin\java --enable-preview HelloWorld HelloWorld() Hello World!
02-04-2024

Able to reproduce on Windows for the following JDK versions. JDK 21.0.2 : Passed - Only HelloWorld! is printed JDK 22-ea+9-633 : Failed - Both HelloWorld() and HelloWorld! is printed JDK 22.0.1-ea : Failed JDK 23-ea+6-400 : Failed
02-04-2024

The observations on Windows 11: JDK 22: Passed, only Hello World! observed. JDK 23: Passed. On Ubuntu: JDK 22: Passed. Closed as can not reproduce.
01-04-2024