I've searched on this here and I can't find the answer. My question is pretty basic. So far I've gotten a few applets to compile and run, but when I try make a program out of them I can't make it happen. I want to be able to just type java "classname" and run it without using applet viewer or a browser. I'm adding the "public static void main (String[] args)" line I can't get them to compile, let alone run as a program. I get the following error: 'class' or 'interface' expected. It doesn't seem to matter where I add this line. I would appreciate any help anyone can provide. Thanks in advance.
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
John Can you post the code where the error is occuring? It sounds like some sort of a syntax error, you might a brace out of place or a declaration that looks like a class or interface to the compiler but it isn't.
Dave
John Ringo
Greenhorn
Joined: Feb 22, 2002
Posts: 17
posted
0
I've tried to add the main method right before and after the CalcEvent class declaration, but no luck. Thanks! [ May 07, 2002: Message edited by: Dirk Schreckmann ]
Ria Mathur
Greenhorn
Joined: Oct 11, 2001
Posts: 14
posted
0
The main() method needs to be inside the the class. Hope it helps, - Ria
-Ria
Roy Ben Ami
Ranch Hand
Joined: Jan 13, 2002
Posts: 732
posted
0
its not that easy.... you need to extend from Frame instead of Applet. also add these lines: setVisible(true); setBounds(0,0,350,350); in the main method you need to create an object of your classand activate the start method on it. so:
also the main should be in the class as been pointed out.
John Ringo
Greenhorn
Joined: Feb 22, 2002
Posts: 17
posted
0
Thanks for the replies Ria and Roy. When I do all that it compiles okay. It's actually making more sense to me now, and I thank you both for that. When I do exactly what Roy said about extendend from Frame, setBounds, setVisible, and making the main object it compiles and I get the follwing when I run it with "java CalcEvent.": Exeption in Thread "main" Java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:439) at java.awt.Container.add(Container.java:3100 at CalcEvent.start(CalcEvent.java:82) at CalcEvent.main(CalcEvent.java:186) I've tried to add a throws clause for the start and main, but no it didn't work... maybe I should use a try/catch... I have no idea what to do about two AWT exceptions anyway. Any more ideas would be apprecaited.
Roy Ben Ami
Ranch Hand
Joined: Jan 13, 2002
Posts: 732
posted
0
ok my mistake. make sure u add the lines setVisible() and setBounds() i told you about to the end of the init() method. also call the init() method before you call the start() method like this:
John Ringo
Greenhorn
Joined: Feb 22, 2002
Posts: 17
posted
0
Thanks for the help Roy... works fine. The best thing is I actually understand it now, I think. I had to put the setVisible in both the init and start methods. Thanks again. It's fun to see this stuff actually work.