Is main method compulsory in Java?

The answer to this question depends on version of java you are using. Prior to JDK 5, main method was not mandatory in a java program.

  • You could write your full code under static block and it ran normally.
    However, From JDK6 main method is mandatory. If your program doesn’t contain main method, then you will get a run-time error “main method not found in the class”. Note that your program will successfully compile in this case, but at run-time, it will throw error.

    // This program will successfully run
    // prior to JDK 5
    public class Test 
    	// static block
    		System.out.println("program is running without main() method");


  • If run prior to JDK 5
    program is running without main() method
  • If run on JDK 6,7,8
    Error: Main method not found in class Test, please define the main method as:
       public static void main(String[] args)
  • This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Please write to us at to report any issue with the above content.

Recommended Posts:

1.4 Average Difficulty : 1.4/5.0
Based on 28 vote(s)