main
method, which serves as the starting point of the entire program. The main class can be not only a class but also an interface or an enum. For a JavaFX application, the main class must implement javafx.application.Application
.The
main
method must be public static
. Additionally, it can have the strictfp modifier. There are no restrictions on annotations or exception lists.The main method must declare a single argument - a string array. Both
String[]
and String...
compile into the same bytecode, so either is acceptable. The array name can be anything, and its value will contain the command-line arguments.When an application is launched via the classpath, the main class is specified as a command-line parameter. If a single source file is executed, it describes the main class.
For an executable JAR file (
java -jar MyJar.jar
), the main class must be specified in the manifest. Inside the archive, the line Main-Class: com.itsobes.MyClass
is added to the META-INF/MANIFEST.MF
file. Otherwise, the launch fails with the "no main manifest attribute" error.If the specified main class does not contain a method that meets all the main method criteria, the program will crash with the error "Main method not found".
In applets, instead of
main
, the entry points are the init
and start
methods. Since Java 9, the applet technology has been deprecated, and as of Java 11, it has been completely removed. We won't go into further detail on applets.