Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

applet vs. program?  RSS feed

 
John Ringo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Ringo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method needs to be inside the the class.
Hope it helps,
- Ria
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!