Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what will happen if i do not write the main function in my program???

 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly would happen if I did not write the main() function in my program?. Is it possible to write program without it?
Thank you.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it?
What happened when you tried to call your program?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, keep in mind that main() and main(String[]) are two different methods. The latter is the one that will be called by the JVM.
[ January 08, 2008: Message edited by: Ben Souther ]
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am tried it now it gives no complile time error, but when i tried to run it it gave an error that says: Exception in Thread main java.lang.noSuchmethodError;main

ok , i think it wont work obviously. I dont know the difference between main() and main(Strig[] args). would you please tell me.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12198
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, the obvious answer is that one takes a String array as a parameter, and the other doesn't. The one that does take the parameter is the one that is called when you try and start up things. if you don't write it, your program can't start.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in addition to the String[] argument, main used as an entry method must be public and static, with a return type of void.

I can't resist tossing out this Java parlor trick...
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Note that in addition to the String[] argument, main used as an entry method must be public and static, with a return type of void.

I can't resist tossing out this Java parlor trick...


I can't believe how could this code give an output.What is the trick behind it. can you explain it to me.
thanks
 
manoj r patil
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will execute the static initializer block before it starts looking for a main method. This is how JVM works ...
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Note that in addition to the String[] argument, main used as an entry method must be public and static, with a return type of void.

I can't resist tossing out this Java parlor trick...


There is a trick hidden in that code.
Try removing that System.exit(0); from the code and compile and run the code,then you will get a Exception after printing the message
Exception in Thread main java.lang.noSuchmethodError;main
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Balasubramanian Chandrasekaran:
... Try removing that System.exit(0); from the code and compile and run the code,then you will get a Exception after printing the message Exception in Thread main java.lang.noSuchmethodError;main

Right. The class loads and the static block executes, providing the output. The call to System.exit(0) shuts down the program before it realizes there is no main method.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well marc

i was not able to see the What? No main method? in my console during the compilition
but my .class file is generated i think it is supported in lower version of

java below 1.5.0_06

kindly suggest me
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys i got it
i didnt set the class path
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic