My next Java question

Discussion in 'Mac Programming' started by larswik, Sep 12, 2006.

  1. larswik macrumors 68000

    Sep 8, 2006
    I wrote this from the example in the book.

    import javax.swing.JOptionPane;

    public class Greeter
    public void sayHello()
    JOptionPane.showMessageDialog(null, "Hello World","test", JOptionPane.INFORMATION_MESSAGE);

    I then typed in the Terminal, javac the new command line popped up and I could see in the folder it created a file called Greeter.class

    I then typed in, java Greeter

    I was expecting a window to pop up sayign what it said in the book but here is what the termianl came back with.

    larsg5s-power-mac-g5:~ larsg5$ java Greeter
    Exception in thread "main" java.lang.NoSuchMethodError: main
    larsg5s-power-mac-g5:~ larsg5$

    I looked over the code and it looks ok, I think. The book didn't say to type this out I just tried it for fun, but I thought it would have worked.


  2. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    You need the other file that contains the main() function, that instantiates a Greeter object.
  3. bookofjames macrumors newbie

    Dec 11, 2005
    I think you need to seriously do some Java tutorials to get the hang of it.

    This link may be of help.

    main runnable method is a pretty basic concept. In order to run any java class as an application, it needs to implement the void main() method.

  4. savar macrumors 68000


    Jun 6, 2003
    District of Columbia
    The problem, as the error message says, is that there is no function called "main".

    The way that the "java" command works is that it opens up your class and executes a static method called "main". Every application has to have this method. (Notice I didn't say "every class" -- there's a big difference. Java applets are also different.)

    The easiest way to fix your program is to change the line

    public void sayHello()


    public static void main(String args[])

    That should do it! Good luck--
  5. larswik thread starter macrumors 68000

    Sep 8, 2006
    Ahh, ok I see, I jumped a head. They were giving examples of coding and alot of what you said was in the coding on the previous page. I got a little excited when I saw that I could get a result on an external window instead of the Terminal replying "Hello World".

    I guess it needed some of the coding before that one. It was not an example that I was supposed to write in the tutorial. But instead I thought it would be fun to see something work that wasn't a Terminal responce.

    I will follow the book more closly since I seem to be getting myself into troube more often.

    Thanks again folks!


Share This Page