Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

NoSuchMethodError

  Asked By: Jose    Date: Apr 21    Category: Java    Views: 518
  

Does anyone know this error: "Exception in thread "main"
java.lang.NoSuchMethodError: main" ?

I get it when trying to run "Party.Class", which compiled fine from
the following code:

class Party {
public void buildInvite(){
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
}} //more code...

(Note: said code is from Page 3 of Head First Java, with a few
corrections.)

Any info helpful...I am an extreme newbie, so am probably missing
something here. Like isn't there supposed to be a "main()"
statement? (And did I forget to set some class parameter in my
autoexec.bat when I set up J2SE v 1.4.2_03 (SDK) on my Win98SE
machine?)

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Nicholas Wells     Answered On: Apr 21

Do you have a main  method or the Party constructor?

If you did it would look something like

class Party {
public static void  main(String[] args){
Party p = new Party();
p.buildInvite();
}

public Party(){
}

public void buildInvite(){}
}

Note that you don't actually need a constructor with no arguments, but I
find that it is good form to put them in until your brain positively
knows what is happening.

Also do you have the directory in question in the class  path, or failing
that "." in your classpath (. is the current directory).

 
Answer #2    Answered By: Lily Brown     Answered On: Apr 21

TO run  java class, the main  function must be there.

Add following method to your class

public static void  main (Sting args[]) {
// code  to be executed when this class  runs
}

 
Answer #3    Answered By: Umaiza Hashmi     Answered On: Apr 21

you have't declared the main  method...
The compiler will compile without any error, since compiler won't care about
the main method..
At runtime it will look for the main method which is not there...
So u need a main method which inturn it creates reference of the Party
class.

 
Answer #4    Answered By: Barachias Levi     Answered On: Apr 21

Thanks so much everyone for your answers. I'm just a newbie  here, so
I'm surprised and happy to see such a response.

Regarding the NoSuchMethodError message I was getting, it probably
was the fact that I had no main() argument in the code.

I wrote to O'Reilly and they admitted that that piece of code  did not
work. They wouldn't say why, tho.

I have a long way to go with Java, so I'm especially anxious to start
writing code. Hopefully soon!

 
Answer #5    Answered By: Naomi Lee     Answered On: Apr 21

Okay, I'm an extreme newbie  as well, but I think I know your problem. In
order to run  a java  application, you have to have one method named
"main":

public static void  main(String[] args){
//do something, such as invoke your method above
}

It is okay to have a class  without a main  method, but only if you don't
try to invode that class directly (that is, you import it into other
code.)

 
Didn't find what you were looking for? Find more on NoSuchMethodError Or get search suggestion and latest updates.

Related Topics:




 
 
 

Related Post