• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beginner's question

 
Michael Weber
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compile a simple java application but getting this error:
"Exception in thread "main" java.lang.NoSuchMethodError: main"

I am pretty sure that the code is ok. I used the same one as in the book What could be the error
Thanks in advance
 
Michael Weber
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code (just in case):

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);
}
}

And the error I posted earlier - I get that when I try to run the application (it has already been successfully compiled).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

The class you're showing isn't a complete program, so it can't be executed by itself. Someplace in every Java application, there has to be a method

public static void main(String[] args)

When you run a class from the command line, Java looks in that class for this method and calls it, or reports the error you're seeing.

I'd tell you to just add a main method, but you still wouldn't see anything; the method you're showing creates a window but never makes it visible, so there's no point in calling it. Here's about the smallest complete program based on this code:

 
Fahd Shariff
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call pack on a Panel and you should be making the Frame visible like so:

 
Michael Weber
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Fahd and Ernest!
Problem solved
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic