This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner Watch "beginner New topic
Author

beginner's question

Michael Weber
Greenhorn

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
Greenhorn

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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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
Greenhorn

Joined: May 11, 2005
Posts: 15
Thanks a lot Fahd and Ernest!
Problem solved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginner's question
 
Similar Threads
Exception in Main
toString()?
Using throws Exception I'm getting an error
what will happen if i do not write the main function in my program???
error with EJB first example