How to run a java program without main function.

Recommended Answers

All 3 Replies

there is no "main function" in Java, only a "main method".
It is required to have the exact signature "public static void main(String[] args)" or a lexical equivalent (it can for example be made final, or you may use another name instead of args).

Without that the JVM will not start executing the program.

There is one way to get something done, but that's NOT running a program.
That way involves creating a static initialiser block in the class, which will be executed at class loading time.

This smells horifically like a homework question so I won't show you how to do it :)

you can run it without main method, but then it have to be Applet

Am'I correct jwenting?

An applet isn't technically a program :)
Same as a servlet or an EJB, both of which run without a "main" method.
Same for a SOAP service.
All can exist only within a specific runtime environment, and are effectively plugins to that environment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.