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

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • 27
    • Component/s: tools
    • None
    • behavioral
    • low
    • This is presumably a rare cases, distinct from the primary cases for the improved launcher protocol.
    • add/remove/modify command line option
    • Implementation

      Summary

      The Java launcher runs an incorrect main method in some rare cases. This request proposes to fix that.

      Problem

      JLS 12.1.4. (Invoke a main Method) says:

      A main method declared in, or inherited by, a given class is a candidate main method if either:

      and JLS 8.2 (Class Members)

      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.

      i.e. class members that are package-private are not inherited to subclasses that reside in packages different from the package in which the superclass resides.

      Then having:

      package base;
      public class Base {
          void main(String... args) {
              System.err.println("Incorrect main method!");
          }
      }
      
      package test;
      public class Test extends base.Base {
          void main() {
              System.err.println("Correct main method!");
          }
      }
      
      $ java test/Test.java
      Incorrect main method!
      

      This is incorrect per the specification above - the Base.main(String[]) method is package-private, and hence is not inherited to test.Test, and hence should not be called. test.Test.main() should be called instead.

      Similarly, in case of:

      package base;
      public class Base {
          void main(String... args) {
              System.err.println("Incorrect main method!");
          }
      }
      
      package test;
      public class Test extends base.Base {
      }
      
      $ java test/Test.java
      Incorrect main method!
      

      While the correct behavior would be:

      $ java test/Test.java
       error: can't find main(String[]) or main() method in class: test.Test
      

      (or the corresponding error for the classfile-based launcher)

      Solution

      The proper inheritance rules will be followed when detecting the main methods to invoke.

      Specification

      No changes to formal specifications. The java launcher implementation will follow the existing specifications when looking for the main method.

            Assignee:
            Jan Lahoda
            Reporter:
            Jonathan Lampérth
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: