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.

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
vickysoni
12 years ago

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

Irfan Khan
13 years ago

Its amazing…

Constant Concept
16 years ago

i suppose it 😀