• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method not found in class HelloWorld

 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am VERY new to applets, so bear with me...

I am attempting to compile the following example program:



However I keep getting an error message:  main method not found in class HelloWorld

I know I need to create an HTML code for the Hello World Applet, however I would like to understand why.

Could anyone explain this to me?
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sally,

How are you running this applet? 
The error message you are getting indicates you are trying to run it as a standard java program, rather than a java applet, using the "java" tool.

To compile the applet, I would expect you to use javac:  javac HelloWorld
That should give you a class file.

Applets were intended to be run within a web browser - within a web page. 
Hence you have to write an HTML page to "run" it.

Rather than "java" you would use the "appletviewer" tool to run it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason that you are creating an applet? They're pretty much obsolete and disabled by default in modern browsers.
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing it for an assignment.

Yes, I was running the applet as javac HelloWorld.java and then java HelloWorld

I created the following HelloWorld.html file:



I think I got it to work.
 
Piet Souris
Rancher
Posts: 1526
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make an Applet run as if it were a stand alone program. Then you can choose how to run it.
You need to add a main() routine to your Applet, for instance:
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also there is a tool in the JDK for executing applets:  Appletviewer.
It will read and use the <applet tag from within your source.  For example here is the front of a source file (SpaceShip.java) with the <applet tag.:

To test this code I enter: AppletViewer SpaceShip.java

Note: The code in the last post treated the applet as if it were a Panel (Applet class extends Panel).  If you had code in the init() method, it would have to be called. 
It is possible for a desktop app (ie a class with a main method) to provide most of the features that an applet requires, but the code is a lot more involved.
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:You can make an Applet run as if it were a stand alone program. Then you can choose how to run it.
You need to add a main() routine to your Applet, for instance:


This is a really great suggestion, however I tried this approach most of the weekend and continually received error after error. I ended up defaulting to the direct applet coding.
In particular, the setPreferredSize statement was particularly worrisome. I never did figure out why.
 
Tim Moores
Bartender
Posts: 3129
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:You can make an Applet run as if it were a stand alone program.


Sally Jenkins wrote:This is a really great suggestion,


Not really. Creating an instance of a class extending Applet doesn't make it an applet, there's more to it. Norm's suggestion of embedding the applet tag in the source code is a good one, that way you can run the applet using appletviewer without needing an extra HTML file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic