Meaningless Drivel is fun!
The moose likes Applets and the fly likes Can't run java HelloWorld Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Can Watch "Can New topic

Can't run java HelloWorld

Lydia Zhang

Joined: Feb 24, 2002
Posts: 29
Hi guys,
I made my first Applet "" and "Hello.html" files into the same folder C:\Java.
When I run from the CP, C:\Java>javac's OK.
Then I run C:\Java>java HelloWorld
It gave me an error called Exception in thread "main" java.lang.NoSuchMehtodError.main
But if I run C:\Java>appletviewer Hello.html instead, It works.
My file code is as following:
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet{
public void paint(Graphics g){
g.drawString("Hello World!", 50, 25);
The HTML file is:
<TITLE>A Simple Program</TITLE>
Here is the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
Is there anything wrong?

Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Wrong, no. Missing, yes.
In order for your code to execute as both an applet and an application, you need to incorporate a main method into your code.
Also, a browser or a program that just runs an applet has to a way to render graphics. A main method doesn't do that automatically; you need to add it.
Consider this:

In this example, I've constructed an object of type HelloWorld in the main method, then created a Frame to put the HelloWorld object in. After giving the frame size and visibility, I call up a graphics context so it can be passed to the applet's paint() method.
What we do in this main() method is a skeletal example of what a browser does and what appletviewer does. Now you can run this program either way.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Lydia Zhang

Joined: Feb 24, 2002
Posts: 29
Thanks Michael, it works well now! Also I am reading the book "Complete Java2 Certifucation Study Guide" these days, and plan to take the exam in August. It's a great book, you did a great job. Thanks!
I agree. Here's the link:
subject: Can't run java HelloWorld
jQuery in Action, 3rd edition