*
The moose likes Beginning Java and the fly likes what will happen if i do not write the main function in my program??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what will happen if i do not write the main function in my program???" Watch "what will happen if i do not write the main function in my program???" New topic
Author

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

Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
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.


****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you tried it?
What happened when you tried to call your program?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 29, 2007
Posts: 60
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

Joined: Oct 02, 2003
Posts: 11160
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
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

Joined: Jun 06, 2002
Posts: 181
This will execute the static initializer block before it starts looking for a main method. This is how JVM works ...


love your job and not your company;
...because you never know when your company will stop loving you!
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 28, 2006
Posts: 236
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

Joined: Sep 28, 2006
Posts: 236
hey guys i got it
i didnt set the class path
 
jQuery in Action, 2nd edition
 
subject: what will happen if i do not write the main function in my program???
 
Similar Threads
Re:Class.forName()
pop up window hides itself when parent is closed
Writing from variables into a file
JNI on Linux----error
database