Java Launcher does not fail when running compact java-file with private no-arg constructor

XMLWordPrintable

    • Type: CSR
    • Resolution: Approved
    • Priority: P4
    • 26, 27
    • Component/s: tools
    • None
    • behavioral
    • minimal
    • Hide
      JEP-512 specifies that for instance main method, the corresponding class must have a non-private constructor with no parameters. It further states that if there is no such constructor then the launcher reports an error and terminates. The change here fixes the implementation to comply with the specification.

      It would be unexpected to see any application relying on a private constructor to be invoked for instance main method. So there shouldn't be any compatibility impact.
      Show
      JEP-512 specifies that for instance main method, the corresponding class must have a non-private constructor with no parameters. It further states that if there is no such constructor then the launcher reports an error and terminates. The change here fixes the implementation to comply with the specification. It would be unexpected to see any application relying on a private constructor to be invoked for instance main method. So there shouldn't be any compatibility impact.
    • Other
    • JDK

      Summary

      Correct the java source launcher to reject programs with a private no-arg constructor, when launching instance main method.

      Problem

      The behaviour of the java launcher for the following HelloWorld.java program defining a private no-arg constructor is different when run in source and class mode:

      class HelloWorld {
        private HelloWorld() {}
        void main() { IO.println("Hello world"); }
      }
      

      $ java HelloWorld.java

      Hello world
      

      javac HelloWorld.java | java HelloWorld

      Error: no non-private zero argument constructor found in class HelloWorld
      remove private from existing constructor or define as:
         public HelloWorld()
      

      Solution

      Perform same check as in class launch mode and fail with a similar error message.

      The check in class mode, found in LauncherHelper.java, reads:

          if (Modifier.isPrivate(constructor.getModifiers())) {
              throw new Fault(Errors.CantAccessConstructor(mainClassName));
          }
      

      That check should be adopted and applied to SourceLauncher.java as:

          if (Modifier.isPrivate(constructor.getModifiers())) {
              throw new Fault(Errors.CantUsePrivateConstructor(mainClassName));
          }
      

      Specification

      No changes to the specification.

            Assignee:
            Christian Stein
            Reporter:
            Robert Scholte
            Chen Liang, Jaikiran Pai
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: