Source launcher instantiates wrong class on inherited instance main

XMLWordPrintable

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

      In the source launcher the candidate main method is executed in the context of the class/interface/abstract class it is inherited from, rather than in the context of the actual intended main class:
      In the following example we should call `new A().main()`, but rather `new B().main()` is called.
      Example 1:

      ```
      // A.java
      class A extends B {}

      // B.java
      class B {
          void main() {
              System.out.println(getClass().getName());
          }
      }
      ```

      ```
      $ java A.java
      B // expected "A" here

      $ java B.java
      B
      ```

      When using the same example with `javac A.java` and `java A` we get the expected output of "A"

      **Additional notes:**
      - If B is an abstract class or interface currently we get the following:
      `error: abstract class: B can not be instantiated`
      - If B is inside a module which is not open to reflection:
      Exception in thread "main" `java.lang.reflect.InaccessibleObjectException: Unable to make void b.B.main() accessible: module b does not "opens b" to module jdk.compile`

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

              Created:
              Updated: