• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginner's question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Fahd and Ernest!
Problem solved
reply
    Bookmark Topic Watch Topic
  • New Topic