Issue
I'm using Java 11 and IntelliJ. See the snippet below:
public class Outer {
public static class InnerA {
public static void main(String[] args) {
System.out.println("A");
}
}
public static class InnerB {
public static void main(String[] args) {
System.out.println("B");
}
}
}
This results in no compile errors, but I cannot run it. Running it in the IDE result in a ClassNotFoundException
I tried using bash:
$ java Outer$InnerA.java
Which yields:
error: can't find main(String[]) method in class: Outer
I've tried rebuilding the project with no success.
Edit:
Since java 11 it is possible to execute a class without compiling it (javac
). If I add a main()
to Outer.java
and execute it in the IDE or if I type the following in bash:
java Outer.java
Then the main()
of Outer.java
class is successfully called.
Solution
I found an answer myself.
IntelliJ:
I was using Gradle as my build tool. In IntelliJ this seems not to work. After I've change it to "IntelliJ IDEA" (see screenshot), I was finally was able to run it.
Bash:
Considering the new Java 11 feature i.e. "launching single-file programs", it seems it's not possible to run an inner static class like this way.
So after compiling and escaping the "$", I was able to run the file with:
$ java Outer\$InnerA
Thanks to user for hinting into the right direction.
Answered By - Younes El Ouarti
Answer Checked By - David Marino (JavaFixing Volunteer)