Write a Java Application without a main() method

Here we are.. this time with a small Java tip.. being Java fans..we just cant wait to post on Java. So, decided to start of with something which most of us might not be aware of.

You can write a runnable Java program which does not have main method at all. This can be done using the static block of the class.

The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.

<font color="#7f0055">class </font><font color="#000000">MainMethodNot</font>
<font color="#000000">{</font>
<font color="#ffffff"> </font><font color="#7f0055">static</font>
<font color="#ffffff"> </font><font color="#000000">{</font>
<font color="#ffffff"> </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">"This java program has run without the main method"</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#ffffff"> </font><font color="#000000">System.exit</font><font color="#000000">(</font><font color="#990000">0</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#ffffff"> </font>
<font color="#ffffff"> </font><font color="#000000">}</font>
<font color="#000000">}</font>

  • Share/Save/Bookmark

Related posts:

  1. How do I declare a constant in Java
  2. Java: Lets revisit the past
  3. Java Roundup
  4. [Java] Why (and how) to comment your code
  5. [Java] Reading a Webpage through URL

No Responses to “Write a Java Application without a main() method”

  1. Intresting :) thanks

  2. Interesting. But, would be helpful if the situations where this technique can be used?

  3. Vivek Srivastav on October 3rd, 2007 at 12:38 am

    it does not work buddy….I get NoClassDefFoundError Exception

  4. Hi Vivek,

    Ensure that the class name and the name of the file is correct. Java is case sensitive. Also ensure that you have the class in your classpath.

    Cheers,
    Vaibhav

  5. [...] blogpost described the difference between JDK and JRE and then in the series of posts like Write a Java Application without a main() method which explained the trick of writing a program without a main method ( though this certainly is not [...]

  6. if we can write a java program without a main() function then what is the need of main function…..

  7. Thanks Manish for bringing that up. It definitely is an interesting question. However, the issue with this kind of program is that without a main() method, though you can write this short code; its pretty much useless in terms of productive work. I hope you got it. See the exit(0) statement which just exits the system.

    Read: The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.

    Cheers
    Vaibhav

  8. Sir, can u tell me that Where JVM exist in computer.

    and which productive work . can u give some eg. please……

  9. Manish Kr. Chauhan on July 14th, 2008 at 8:09 pm

    hi!!!

    JVM exist inside the JRE called java run time environment…

Leave a Reply