tags:

views:

34

answers:

1

I am having trouble with a HelloWorld Applet.

Here is my Java code:

package webCrawler.applet2;

import javax.swing.JApplet;
import java.awt.Graphics;

public class HappyFace extends JApplet
{
    public void paint (Graphics canvas)
    {
        canvas.drawOval(100,50,200,200);
        canvas.fillOval(155,100,10,20);
        canvas.fillOval(230,100,10,20);
        canvas.drawArc(150,160,100,50,0,180);
    }
}

Here is my index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
    <head>
    </head>
    <body bgcolor="000000">
        <center>
            <applet
                code    = "HappyFace.class"
                width   = "500"
                height  = "300"
                >
            </applet>
        </center>
    </body>
</html>

In Eclipse if I go: Run -> Run it works, however if I do this:

% pwd
/Users/me/Documents/workspace/WebCentric/bin/webCrawler/applet2
% ls
HappyFace.class         index.html
% open index.html 

It opens the html page in Firefox but the app does not work:

app not working

Update:

As Pablo Santa Cruz suggested I:

  • Changed the location of index.html
  • Changed index.html to be code = "webCrawler.applet2.HappyFace"

This is the exception in the console.

java.lang.UnsupportedClassVersionError: webCrawler/applet2/HappyFace (Unsupported major.minor version 49.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:213)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:151)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:680)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:635)
    at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1991)
    at jep.AppletFramePanel.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:564)
    at sun.applet.AppletPanel.run(AppletPanel.java:301)
    at jep.AppletFramePanel.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:552)
+1  A: 

In your code attribute in the applet tag, you should use FQN of the class: webCrawler.applet2.HappyFace (remove .class extension also).

Also, make sure you have your .class file inside the appropriate directory (package) structure. In your case, the HTML files should be in:

/Users/me/Documents/workspace/WebCentric/bin/

And only your HappyFace.class file should be in:

/Users/me/Documents/workspace/WebCentric/bin/webCrawler/applet2

Also, do keep in mind that there is a Java console on the Browser (IE, Firefox, Chrome, Safari) you can take a look at to see what's the error you are getting on applet execution.

UPDATE:

Your Exception is saying that you compiled your code with a newer version of Java than the JVM supports. I.E. you used Java 6 compiler in Eclipse but your Java Browser Plugin is only Java 5 (your JRE is only Java 5).

I would change project settings in Eclipse, tell it to use Java 5 compatible compiler (there is an option for that), compile and deploy the Applet again. It should work.

Pablo Santa Cruz
changed it to `code = "webCrawler.applet2.HappyFace"` but still no dice.
sixtyfootersdude
did you also moved your files around as I suggested?
Pablo Santa Cruz
Yep I did, See my update.
sixtyfootersdude
Updated the answer... Check it out.
Pablo Santa Cruz
Awesome! Thanks for the help!
sixtyfootersdude