Main Tutorials

How to run a Java application with no main method

Java application usually required a main() method as entry point to run it. Here’s an example by using static initializer to run a Java application even without the main() method.

P.S The static initializer is called while the Java class is loaded.

This is for fun only, do not use this example in real environment as a replacement for main() method, static initializer is not created for the Java main() application entry point purpose 🙂


package com.mkyong.io;

public class App{

     static
    {
        System.out.println("Magic here ~");
        System.exit(0);
        
    }
}

Run the above Java application, it will print out the “Magic here ~” to console.

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
vickysoni
10 years ago

its give an exception at run time selection dose not contain main method

Irfan Khan
11 years ago

Its amazing…

Constant Concept
14 years ago

i suppose it 😀