Static block is not executing in JDK 7, “Main method not found”, but works in JDK 1.5

前端 未结 9 2138
我寻月下人不归
我寻月下人不归 2021-01-07 20:19

I have written a simple class with one static block

class Hello
{

  static {
           System.out.println(\"Hello\");
       System.exit(0);
     }
}
         


        
9条回答
  •  时光取名叫无心
    2021-01-07 21:00

    before java 1.7 static block is execute before the main method so we can execute code without main mehod, Since JDK 1.7, it is not possible to execute static bock without the main method,because compiler is looking for main method in class file.

    so when we want to execute code in above JDK 1.7 it show Error: Main method not found in class hello, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

提交回复
热议问题