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!

Alex
  • 3,111
  • 6
  • 27
  • 43
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
  • Add [SSCCE](http://sscce.org) example to your post. – MockerTim Apr 10 '14 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. – Noble-Surfer Apr 10 '14 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. – Noble-Surfer Apr 10 '14 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](http://stackoverflow.com/questions/5947063/how-to-specify-correctly-codebase-and-archive-in-java-applet/5950258#5950258). You'll find the answer there. – MockerTim Apr 10 '14 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](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 11 '14 at 05:00
  • `resize(150,25);` The size of an applet is set in HTML. The applet code should not try to enforce a size. – Andrew Thompson Apr 11 '14 at 05:01

2 Answers2

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
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
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.

MockerTim
  • 2,475
  • 1
  • 25
  • 31
  • 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- – Noble-Surfer Apr 10 '14 at 13:30
  • I assume I will need to create my own? I've tried to do this, following the steps at http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm but I'm still having the same issue when trying to view the page in a browser... any suggestions? – Noble-Surfer Apr 10 '14 at 13:30
  • @someone2088 You don't need to create the jar file, unless you have more then one class. – MockerTim Apr 10 '14 at 13:35
  • 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 – Noble-Surfer Apr 10 '14 at 13:37
  • @someone2088 And what happens when you click that message? – MockerTim Apr 10 '14 at 13:40
  • I get a 'ClassNotFoundException'... clicking that error opens a 'Java Console' popup window which lists a load of options such as "c: clear console window", "f: finalise objects on finalisation queue". – Noble-Surfer Apr 10 '14 at 13:43
  • Interestingly, I've just noticed that the popup window states my computer's username as its home directory- not the folder I'm working from, which is a few levels down- could this be why it's not finding the class? If so, how do I specify the folder that my classes are saved in as the one for it to search in? – Noble-Surfer Apr 10 '14 at 13:44
  • @someone2088 Have you added the codebase attribute to your applet tag? – MockerTim Apr 10 '14 at 13:55
  • The only attribute I currently have attached to my applet tag is the 'code' one- is 'codebase' different? What should I be looking to assign to the 'codebase' attribute? – Noble-Surfer Apr 10 '14 at 15:03
  • @someone2088 Have a look at Andrew Thompson's answer. – MockerTim Apr 11 '14 at 05:45
  • @someone2088 About codebase you can read [here](http://www.tutorialspoint.com/java/java_applet_basics.htm) and in the SO post, I gave you the link to earlier ([how-to-specify-correctly-codebase-and-archive-in-java-applet](http://stackoverflow.com/questions/5947063/how-to-specify-correctly-codebase-and-archive-in-java-applet/5950258#5950258)). And you can google for it, of course. – MockerTim Apr 11 '14 at 05:52