Mac My next Java question

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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 Greeter.java 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.

Thanks,

-Lars
 

savar

macrumors 68000
Jun 6, 2003
1,956
0
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()

to

public static void main(String args[])

That should do it! Good luck--
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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!

-Lars