Many Java programmer gives you answer that they can run
Java program without main method by writing code in static initializer block,
which is half true. Yes, code written in static initializer block is
executed before calling main method, but you won't be able to run a class by
using Java command, or Eclipse or anything else, until it got public static
void main(String args[]) method on it. If you try to run such programs, you
will get following error :
Error: Main method not found in class test, please define the main
method as: public static void main(String[] args)
public class test{ static { System.out.println("Hellojava, Java progarm without main method"); int x = 20; // Can initialize static variables System.out.println("Variable x : " + x); Thread t = new Thread() { @Override public void run() { System.out.println("Started thread from static initializer block"); System.out.println("Thread Finished"); } }; t.start(); } public static void main(String args[]) { // Empty main method } }
|
That's all about whether you can run a Java program
without main method in Java or not. In short, Yes, you can run a Java program
without main method in a managed environment e.g. Applet, Servlet, and MIDlet,
which runs under control of browser, server and mobile device, but can't run a
core Java program without public static void main(string args[]) method. JVM
will not allow you to execute those methods.
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.
public class test { static { System.out.println("Hello, java"); } } |
note: you can avoid the NoSuchmethodError
by simply calling System.exit(0) immediately after printing the message.
Up to and including Java 6 it was possible to do this using the Static
Initialization Block as was pointed out in the question Printing message on
Console without using main() method. For instance using the following
code:
public class test { static { System.out.println("Message"); System.exit(0); } } |
The System.exit(0) lets the program exit before the JVM
is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the
following error will appear when you try to execute it:
The program compiled successfully, but main class was not found. Main class
should contain method: public static void main (String[] args).
Core Java Interview Questions
Object Oriented Programming(OOP) Questions and Answers
Collections Interview Questions
Java Exceptions Interview Questions
Java Threads Interview Questions
Collection framework in java interview questions
oops concepts with example in java
Java Serialization Interview Questions
Top 20 Core Java Interview Questions with Answers
String Interview Question in java With Example
synchronization interview questions
Java Reflection Interview question
Java Executor Framework (JDK 1.5) Interview Questions
JDK 1.7 interview Questations and Answers
Java I/O interview question with example