How can I execute any program in java without main method?

Total Post:126

Points:882
Java 
 2158  View(s)
Ratings:
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.

  1. Post:48

    Points:336
    Re: How can I execute any program in java without main method?

    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.

      Modified On Mar-29-2018 04:17:39 AM

Answer