1

I'm trying to create a basic Java applet that will display the output of my Java program in a web browser.

Having never worked with applets before, I thought I would follow a tutorial to try and create a simple "Hello World" applet, just to get a simple understanding of how they work.

I am using the example at: http://www.cs.nccu.edu.tw/~linw/javadoc/tutorial/getStarted/applet/index.html and have followed the steps exactly as described.

However, when I compile the Java source file, although a "HelloWorld" class file appears in my 'Project Explorer' window in Eclipse, I cannot see the class file at all when viewing the root project folder in Windows Explorer- all I see there is my HelloWorld.java file, and Hello.html file.

When I run the HelloWorld.java class in Eclipse, although I get a warning in the console that says:

Warning: Can't read AppletViewer properties file: C:.... Using defaults

the application does run- and a little window pops up titled "AppletViewer:...HellowWorld.class". This window has an 'Applet' menu, with menu items such as Restart, Reload, Stop, Save, etc, and the window displays "Hello World!" in the location specified, and a message saying "Applet started." at the bottom.

But, when I try to view the webpage in a browser, I get a message that says: "Error. Click for details" where the "Hello World" message should be displayed...

My HelloWorld.java class has the code:

package openDis.applet;

import java.awt.Graphics;

public class HelloWorld extends java.applet.Applet {
    public void init() {
        resize(150,25);
    }

    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

and the HTML in the webpage I'm trying to use to display the message is:

<html>
    <head>
        <title>A Simple Program</title>
    </head>
    <body>
        Here is the output of the program:

        <applet code="HelloWorld.class" width=150 height=25></applet>
    </body>
</html>

What am I doing wrong here? What do I need to do to get the output of the program to display in the web page? Thanks for any help in advance!

6
  • Add SSCCE example to your post. Commented Apr 10, 2014 at 11:12
  • Sorry- I'm not sure how to do this? All of the code I currently have is copied into the question exactly as is (I've only just started this project, and just wanted to get some output from my program displaying in a web page to ensure that it works before I go any further. Commented Apr 10, 2014 at 11:20
  • When I click the error message in the browser, to see the details, it says it's getting a 'ClassNotFoundException'. I can see the class file in Eclipse though... so it should be there. Commented Apr 10, 2014 at 11:22
  • You need to properly specify the codebase attribute of the applet tag. Have a look at how-to-specify-correctly-codebase-and-archive-in-java-applet. You'll find the answer there. Commented Apr 10, 2014 at 11:25
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets. 2) Why AWT rather than Swing? See my answer on Swing extras over AWT for many good reasons to abandon using AWT components. Commented Apr 11, 2014 at 5:00

2 Answers 2

1

..have followed the steps exactly as described.

No you didn't. Their applet is in the default package, while yours is in openDis.applet package.

So:

<applet code = "HelloWorld.class" width = 150 height = 25>
</applet>

Should be:

<applet code = "openDis.applet.HelloWorld" width = 150 height = 25>
</applet>

And the structure needs to be:

  • dir (directory)
    • applet.html
    • openDis (directory)
      • applet (directory)
        • HelloWorld.class
Sign up to request clarification or add additional context in comments.

2 Comments

@MockerTim I probably do it too often. ;)
Sorry- missed that completely. Thanks!
0

The code attribute of the applet tag should not contain the .class extension. It must contain the class name only. You also need to properly specify the codebase attribute of the applet tag. Have a look at the Deploying With the Applet Tag tutorial for details.

11 Comments

Hi, thanks for your answer. I've got rid of the .class extension, as you suggested, and looked through the tutorial you linked to, trying what it suggested, and I can now get the Hello World applet running and displaying in a popup window when I run the class directly from Eclipse... But I'm still getting the same error when trying to view the applet in the browser- i.e. the 'error: click for details' message. I noticed that in the code example in the link you provided, it is referencing a JAR file-
I assume I will need to create my own? I've tried to do this, following the steps at help.eclipse.org/indigo/… but I'm still having the same issue when trying to view the page in a browser... any suggestions?
@someone2088 You don't need to create the jar file, unless you have more then one class.
Ok- but I've tried it without the jar file, and I'm still having the same issue- i.e. the "Error: click for details" message
@someone2088 And what happens when you click that message?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.