Can you run the java program without main method?
A java applet application, a web application can run without main method. A Java program can run without using ‘ public static void main(String args[]) ‘ method by using a static block.
The static block gets executed soon after the class is loaded, even prior to the ‘public static void main(String args[]) ‘. JVM searches for main method only after exiting from the static block. JVM throws an exception if main method is not found. To avoid throwing this exception we can use System.exit(0);
The following
example depicts a class without main method; with only static block and System.exit(0).
class WithoutMainMethod
{
static
{
System.out.println(“This class has no public static void main(String args[]) “);
System.out.exit(0);
}
}