Friday, 7 July 2017

Can You Run Java Program Without Main Method?

First thing Java programmers learns is that they need a main method to run, but when they go to any Interview or college viva and asked can you run a Java program without a main method, they surprised like hell. Well, there are actually different types of execution model available in Java, for example, Applets which run on browser doesn't have main method, instead they have life-cycle methods like init(), start() and stop(), which controls their execution. Since Applet is a Java program, you can answer this question in Yes. Similarly, we have Servlet, which runs in a Servlet container, comes as bundled in a web server like Tomcat, or Jetty. Servlet also works on callback mechanism, it has methods like init(), service() and destroy(). Container calls init() if Servlet is first time loaded into memory, calls service() if there is a request to process and calls destroy() before removing it from memory.

Since Servlet is also a Java program, we can say that it runs without main method. Third one on this category is MIDlet, which runs on mobile devices e.g. Nokia, Samsung and others. MIDlet has life-cycle methods like startApp(), pauseApp() and destroyApp(), to start, pause and shut-down mobile applications.

Ironically J2ME has app in their core method, but it was made popular by Android and iPhone. Since MIDlets are also Java program, you can say they run without main method.

Now, if Interviewer persist you with something like this, Can you run a Java program without main method in Core Java, not on managed environment like the case of Applet, Servlet and MIDlet. Answer is NO, Why? I will explain that in next paragraph.

By the way, this is one of the many questions related to main method in Java, you can see the full list of interview question on main method here.

Java program without Main method

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 JavaAppWithoutMain, please define the main
 method as:   public static void main(String[] args)

Oracle Java Certifications, Oracle Java Materials

Though you can run a Java program with empty Main method, in which case only code executed will be from static initializer block. Following is a simple Java program with some code written on static initializer block, including a print statement, a variable initialization and starting a thread. As soon as you remove the main method, it will compile fine but will throw above error, when you try to run this program from command line.

 * Java application to demonstrate whether you can run a program without a main method
 * or not.
 * @author
public class JavaAppWithoutMain {

    static {
        System.out.println("HelloWorld, Java progarm without main method");
        int x = 20; // Can initialize static variables
        System.out.println("Variable x : " + x);

        Thread t = new Thread() {
            public void run() {
                System.out.println("Started thread from static initializer block");
    System.out.println("Thread Finished");

    public static void main(String args[]) {
          // Empty main method


Here is how the output look like, when you run this program from command prompt with empty main method.

Oracle Java Certifications, Oracle Java Materials

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.

Related Posts