Java Program Error [closed]
While I was learning a book, I came across a Java example. When I compiled it, I had no error. But when I ran it , it showed:
Exception in thread "main" java.lang.NoSuchMethodError: main
Since I am beginner in Java, I don't know what it is.
The program is:
import java.awt.*;
import java.awt.event.*;
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);
}
}
If you still want to see the execution of this code try this version:
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);
p.add(B);
p.add(c);
f.add(p);
f.setVisible(true);
}
public static void main(String[] args) {
new Party().buildInvite();
}
}
This code can't be fixed with less work than a complete rewrite. It doesn't have a main method, cause the frame to show or add the panel to it. Find a better tutorial.
All Java programs must have a main
method. This is what the JVM looks for to start your program.
精彩评论