Source code launcher cannot call inherited main from another package/module

XMLWordPrintable

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

      The source launcher in modular mode fails to execute an inherited main method, if it is inherited from a package other than the package of the main class.

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

      /// src/b/B.java
      package b;
      public class B {
        public void main() { IO.println("B"); }
      }

      /// module-info.java
      module anything {}
      ```

      ```
      $ java a/A.java
      Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make public void b.B.main() accessible: module random does not "exports b" to module jdk.compiler
              at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:343)
              at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:319)
              at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:267)
              at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:182)
              at java.base/java.lang.reflect.Method.setAccessible(Method.java:176)
              at jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher.execute(SourceLauncher.java:257)
              at jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher.run(SourceLauncher.java:138)
              at jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher.main(SourceLauncher.java:76)
      ```

      This case can be fixed by modifying the logic in `com.sun.tools.javac.launcher.MemoryContext::newClassLoaderFor` to not only `addOpens` for the package of the main class, but also for the package wherein the main method resides.

      Note: this case works fine if we remove the `module-info.java` as then no module layer is created.

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

              Created:
              Updated: