Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
Of course, you can run the program as
java Helloand you will see the message; however, the command will also fail with a message stating:[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling
System.exit(0)immediately after printing the message.As of JDK6 onward, you no longer see the message from the
staticinitializer block; details here.