How does the JVM use the "Main method" to start a Java program? [duplicate]
main
is not a keyword in java. How does the JVM know to execute the main
method as the starting point of any program?
Solution 1:
main
doesn't need to be a keyword in java in order for the JVM to look for it at the start of execution. There is no conflict with other methods or variables also called main
. This is simply how the JVM spec was designed. It was most likely borrowed from the c language.
Java Specification References: keywords, invoking main.
Solution 2:
It's just specified in the JLS.
12 Execution
...
A Java virtual machine starts up by loading a specified class and then invoking the method
main
in this specified class. Section §12.1 outlines the loading, linking, and initialization steps involved in executingmain
, as an introduction to the concepts in this chapter. Further sections specify the details of loading (§12.2), linking (§12.3), and initialization (§12.4)....
12.1 Virtual Machine Start-Up
A 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 calledTest
....
12.1.4 Invoke Test.main
Finally, after completion of the initialization for class
Test
(during which other consequential loading, linking, and initializing may have occurred), the methodmain
ofTest
is invoked.The method
main
must be declaredpublic
,static
, andvoid
. It must accept a single argument that is an array of strings.