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:
```
// 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`
In the following example we should call `new A().main()`, but rather `new B().main()` is called.
Example:
```
// 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`
- blocks
-
JDK-8377010 Source code launcher cannot call inherited main from another package/module
-
- Open
-
- csr for
-
JDK-8377367 Source launcher instantiates wrong class on inherited instance main
-
- Provisional
-
- links to
-
Review(master)
openjdk/jdk/29550