jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes beginner's question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner Watch "beginner New topic

beginner's question

Michael Weber

Joined: May 11, 2005
Posts: 15
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

Joined: May 11, 2005
Posts: 15
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();

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

Joined: Jul 08, 2003
Posts: 24199

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:

[Jess in Action][AskingGoodQuestions]
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38
You can't call pack on a Panel and you should be making the Frame visible like so:

Fahd Shariff<br />"Let the code do the talking"
Michael Weber

Joined: May 11, 2005
Posts: 15
Thanks a lot Fahd and Ernest!
Problem solved
Consider Paul's rocket mass heater.
subject: beginner's question
It's not a secret anymore!