main() method

main() method

Postby padmajaprasad » Mon Feb 25, 2013 9:27 pm

Can we execute a program without main() method?
padmajaprasad
 
Posts: 5
Joined: Tue Jan 29, 2013 2:53 pm

Re: main() method

Postby skadukuntla » Wed Feb 27, 2013 5:27 pm

In Java program execution starts from static block. So I has placed my code in the static block { …. }. After the execution of the static block JVM searches for the main method. If the main method is absent it will throw main method not found exception. Hence I have terminated my program by calling the exit method so that no error messages are displayed.

example:-
class StaticDemo
{
static
{
System.out.println("this is static block");
System.exit(0);
}
}

The above program will not work from Java SE 7 onwards. In Jdk1.7 the above program will compile fine. But when you try to run it will give the followiing error.
Error: Main method not found in class StaticDemo, please define main method as:
public static void main(String[] args)

It seems that Java 7 specifications does not allow to execute a Java program without main method.

so the answer is upto Java SE 6 it is possible to execute a Java program without main method. But from Java SE 7 onwards it is not possible.
skadukuntla
 
Posts: 1
Joined: Wed Feb 27, 2013 5:06 am


Return to Core Java

Who is online

Users browsing this forum: No registered users and 1 guest

cron