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

前端 未结 9 2084
我寻月下人不归
我寻月下人不归 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 20:57

    Upto JDK1.6:-

    It first load the class and while loading the class static block will be executed. and then check the main method to execute.

    JDK1.7 onwards :

    It checks main method first whether it is available or not.

    if available
       then first execute static and
       then main method will be executed.
    
    if not available
    throw error 
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-07 21:05

    You cannot execute a java program without main method unless otherwise it is an applet or something else. I say your observation on jdks might be wrong

    0 讨论(0)
  • 2021-01-07 21:07

    If you read the JLS Chapter 12 carefully (version 5 or 7), it is not specified when the static initialization of the "main" class should occur. Indeed there is a Bug Report that complains about this.

    What is specified is that the "main" class will be initialized (and the static initializers will be run) before the entry point method is called. That is specified in JLS 12.4.1

    I can't explain why they changed this, or find where they documented the change. But apparently it did change. If you wanted the real explanation you would need to ask the Sun / Oracle engineers responsible.

    (FWIW, I think that this is a good change. Having the static initialization happen and then having the program fail due to the not finding the entry point is unexpected behaviour, and unexpected is bad if there is no good justification.)

    0 讨论(0)
  • 2021-01-07 21:12

    Go through JLS 12.1:

    The Java Virtual Machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings. In the examples in this specification, this first class is typically called Test.

    The initial attempt to execute the method main of class Test discovers that the class Test is not loaded - that is, that the Java Virtual Machine does not currently contain a binary representation for this class. The Java Virtual Machine then uses a class loader to attempt to find such a binary representation.

    Java 7 looks for public static main(String[] args) method , which is entry point for the application and then loads the class , unlike Java 6 which loads the class and then looks for the main method.

    0 讨论(0)
  • 2021-01-07 21:13

    Yes, in jdk7. Static blocks are not first executed. It looks first for the entry point in the application.

    So, it first checks for public static void main(String a[]) , if this method is not present, static block will not be executed.

    0 讨论(0)
提交回复
热议问题