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.

class MainMethodNot
{
static
{
System.out.println("This java program has run without the main method");
System.exit(0);

}
}

Advertisements

10 Responses

  1. Intresting 🙂 thanks

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

  3. 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. hi!!!

    JVM exist inside the JRE called java run time environment…

  10. Hey…Ybhav
    I wana know ‘If I want to catch error message and error no when any .exe file forcible closed” and to handle that code in java,what should I hv 2 do,
    suggestions are welcomed from everyone.
    Reply soon
    Thanx in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: