aspose file tools*
The moose likes Beginning Java and the fly likes Do Java program can run without main function()? If not please solve this problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Do Java program can run without main function()? If not please solve this problem" Watch "Do Java program can run without main function()? If not please solve this problem" New topic
Author

Do Java program can run without main function()? If not please solve this problem

Hari Dhanakoti
Greenhorn

Joined: Jan 04, 2007
Posts: 6
Here is the piece of code which runs without a main function.
Help me in this problem

class EndNow
{
public void p()
{
System.out.println("It also........");
}

}

class MainLess
{
static
{
System.out.println("hi it worked ");
EndNow e = new EndNow();
e.p();
System.exit(0);
}
}


Regards<br />Hari
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Michael Jennings
Greenhorn

Joined: Jul 04, 2007
Posts: 20
hi

when a class is loaded the static initialiser blocks and the (public) static void main() get executed.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!

The first thing i want to say is code which is present in a static block will be executed as the class is initialised.the code given by you is in static block it will definately run when class gets initialised,even if you do not give main method...........

i hope it helps.....
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

There has to be a main method somewhere doesn't there or there could be no entry point.

Kaydell
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Originally posted by Kaydell Leavitt:
There has to be a main method somewhere doesn't there or there could be no entry point.

Kaydell


When you run the command
java Mainless
the JVM will load the Mainless class, run any static blocks and then try to call the main method. However, in this case there is a System.exit() in the static block so the program will exit before it tries to call main. If you take out the System.exit(), you will get an error.


Joanne
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Apart from rather silly hacks like the static initialiser one, a Java program must have a main() method, if it is to be run via the "java" command.

However, the "java" command is not the only way to run Java. Applets start a different way, for one thing. But also you can write your own replacement for the "java" command using JNI. This can load any class it likes and can call any methods it likes.

Writing a replacement for "java" isn't a beginner topic, though!


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

good explanation Joanne Neal !


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
kadhir ravan
Greenhorn

Joined: Jul 11, 2007
Posts: 1
in java applet program can run without main function,but specially java programs run only usin main method
Hari Dhanakoti
Greenhorn

Joined: Jan 04, 2007
Posts: 6
Thanks
Joanne Neal,

For Your Valuable reply. I am now clear explanation with the doubt i had raised.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Do Java program can run without main function()? If not please solve this problem