Java Launcher allows inheriting a package-private main from another package

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • None
    • Affects Version/s: 27
    • Component/s: tools
    • None

      JEP 512 states:
      > If the class declares or inherits a main method with a String[] parameter then the launcher chooses that method.
      > Otherwise, if the class declares or inherits a main method with no parameters then the launcher chooses that method.

      JLS 8.2 states:
      > Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

      Currently the java launcher (both class and source mode) allow for a package-private main method to be inherited from a class from a different package. The main method should never be inherited and thus should not be a valid entry point to the java launcher.

      Example/Reproducer:
      ```
      // a/A.java
      package a;
      import b.B;
      public class A extends B {}
      ```

      ```
      // b/B.java
      package b;
      public class B {
        void main() {
          IO.println(getClass().getName() + " from B's main()");
        }
      }
      ```

      ```
      $ java a/A.java
      a.A from B's main()

      $ javac a/A.java b/B.java
      $ java a.A
      a.A from B's main()
      ```

      Note: this occurs both for instance and static main

      Note: With this a falsely inherited package-private main method with arguments can "hide" a valid main method without arguments.

            Assignee:
            Unassigned
            Reporter:
            Jonathan Lampérth
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: