Home > DeveloperSection > Forums > How can I execute any program in java without main method?
Awadhendra Tiwari

Total Post:126

Points:882
Posted on    July-23-2011 5:55 AM

 Java Java 
Ratings:


 1 Reply(s)
 1613  View(s)
Rate this:

Hi,

I have a  small problem while creating program. This might be a silly question as I am fresher in JAVA. I want to ask that
is it possible to run any program in java which have not main method. Like we can run some program in c and c++ which
doesent have main method. Is it possible in java.

Thanks.



James Smith

Total Post:48

Points:336
Posted on    July-23-2011 6:23 AM

Hi awadhendra,

You can use static block to perform this task and at the end of statements called System.exit(0) to avoid to generate an exception.

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

Thanks.


Don't want to miss updates? Please click the below button!

Follow MindStick