This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Hari Dhanakoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Michael Jennings
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

when a class is loaded the static initialiser blocks and the (public) static void main() get executed.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has to be a main method somewhere doesn't there or there could be no entry point.

Kaydell
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good explanation Joanne Neal !
 
kadhir ravan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java applet program can run without main function,but specially java programs run only usin main method
 
Hari Dhanakoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
Joanne Neal,

For Your Valuable reply. I am now clear explanation with the doubt i had raised.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic