java - Why can't we define main function in static inner classes? -


i have following simple code

public class tester {     static class testerchild {         public static void main(string args[]) {             system.out.println("test");         }     } } 

it compiles fine. when run following error

[aniket@localhost src]$ java tester error: not find or load main class tester 

question why can't define our main method in static inner class?

update1 :

as specified in answers/comments have change code following

public class tester {     public static class testerchild {         public static void main(string args[]) {             system.out.println("test");         }     } } 

i compiled , made 2 class files tester.class , tester$testerchild.class. still getting error

[aniket@localhost desktop]$ java tester$testerchild error: not find or load main class test 

update 2:

ok included current directory in classpath , executed still getting error

[aniket@localhost desktop]$ java -cp . tester$testerchild error: main method not found in class tester, please define main method as:    public static void main(string[] args 

it can run main not using right class. main class not tester tester.testerchild.

in eclipse run without setup command line have use java 'yourpackage.tester$testerchild' syntax others mentioned above.

you need wrap name of class in ''s because on linux/unix shell might think $testerchild variable. if try out in prompt if omit ''s:

error: not find or load main class tester

if need explicitly set classpath can use -cp or -classpath option or can set commandline: set classpath=/somedir


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -