Why can't we define main function in static inner classes?
I have the following simple code
public class Tester {
static class TesterChild {
public static void main(String args[]) {
System.out.println("Test");
}
}
}
It compiles fine. But when I run it I get the following Error
[aniket@localhost src]$ java Tester
Error: Could not find or load main class Tester
Question is why can't we define our main method in static inner class?
Update1 :
As specified in the answers/comments I have change the code to following
public class Tester {
public static class TesterChild {
public static void main(String args[]) {
System.out.println("Test");
}
}
}
I compiled it and it made two class files Tester.class and
Tester$TesterChild.class. But still i am getting error
[aniket@localhost Desktop]$ java Tester$TesterChild
Error: Could not find or load main class Test
Update 2:
Ok now I included current directory in the classpath and executed still
getting error
[aniket@localhost Desktop]$ java -cp . Tester$TesterChild
Error: Main method not found in class Tester, please define the main
method as:
public static void main(String[] args
No comments:
Post a Comment