2
votes

I'm using Java SE 1.6 on Mac OS X 10.5.6. The code for my applet is as follows:

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

public class HelloWorld extends JApplet {

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

}

I compiled this to a .class file. The code for my HTML file is as follows:

<html>

<head>
    <title>Hello World</title>
</head>

<body>
    <applet code="HelloWorld.class" height="300" width="300">
            Your browser is ignoring the applet tag.
    </applet>
</body>

</html>

If I run the applet with appletviewer, it works fine. However, if I open the HTML file in a browser, a square appears for the applet space, but the applet doesn't load. (The "Your browser is ignoring the applet tag." doesn't appear either.) The browser status bar shows two messages: Applet HelloWorld notloaded and Loading Java Applet Failed... I tried the applet with Firefox 3.0.6, Safari 3.2.1, and Opera 9.6.3. None of them could load the applet.

If I try opening the HTML for one of the Java demo applets in /Developer/Examples/Java/Applets, it loads fine in all three browsers.

Is there something I'm missing here, in trying to get this simple applet to load in a browser?

9

9 Answers

2
votes

You do not specify a codebase property in the applet tag, so I'd guess your class can not be found.

Try to enable the java console output window. You can do this in "Java Settings" (use spotlight) under the extended options tab (the one with the tree and many checkboxes). Maybe you can see some more information (like ClassNotFoundException) there. Set the setting to "Enable/Show console". Then it should show up when you start the applet.

2
votes

Unfortunately Apple decided to only release a 64bit VM for Java 6 on OS X. The implication of this is that the browsers have to be linked as 64bit apps. Right now, none of them does (as far as I know). So the "easy" solution is to use Java 5 for applets which you can configure in the Java Preferences application.

1
votes

Phil, The comment about the code base by dhiller triggered something that worked for me. If you put the "HelloWorld.class" file in the same folder as your HTML file, and then set your applet tag to: < applet code="HelloWorld.class" codebase="." align="baseline" height="300" width="300" >, then open the HTML file with your browser, it should work. Mine did. The codebase ="." tells the browser that the applet code is in the same folder as the HTML file. The applet load fails because the browser apparently doesn't know where where the class code is located.

0
votes

I'm not an expert in Web programming, but I think the applet tag is not standard. You have to use the object tag with the proper classid to insert an applet (or pretty much anything).

0
votes

You're not giving the apllet a container etc to show itself in. Here's an example HelloWorld applet:

/*
  File.......: MyHello_JApplet.java
  Description: Basic JApplet example.  (Run by a browser.)
  Programmer.: Michael Thomas
  Date.......: Updated 09/09/01, Orig 09/09/01

*/
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Container;
import java.awt.Color;

public class MyHello_JApplet extends JApplet {

  public void init() {
    Container objContainer = super.getContentPane();
    objContainer.setBackground( Color.white );
  }
  public void paint(Graphics g) {
    g.drawString("Hello World from JApplet (Swing - JApplet).",10,25);
  }
}
0
votes

Thank you for all your answers. Some of them pointed me in the right direction to figure out the source of the problem.

I turned on the Java Console in Java Preferences. When I ran the applet again, the following output is what I received:

Java Plug-in 1.5.0
Using JRE version 1.5.0_16 Java HotSpot(TM) Client VM
MRJ Plugin for Mac OS X v1.0.1
[starting up Java Applet Security @ Fri Feb 06 23:47:20 CST 2009]
java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:177)
at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:605)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:723)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1864)
at jep.AppletFramePanel.createApplet(AppletFramePanel.java:189)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:652)
at sun.applet.AppletPanel.run(AppletPanel.java:326)
at jep.AppletFramePanel.run(AppletFramePanel.java:176)
at java.lang.Thread.run(Thread.java:613)

I installed Java SE 1.6 on my Mac, but I guess it didn't install a 1.6 plug-in. Also, it looks as if .class files get stamped with a version number when they are created. I compiled that applet with version 1.6, but tried to run it with a 1.5 plug-in, thus resulting in the UnsupportedClassVersionError. I re-compiled the applet with version 1.5 and tried running it in all three browsers again. Worked like a charm.

Does anyone know if a 1.6 plug-in is in the works?

0
votes

Don't put quotations around your class name in the HTML file. Every time I put quotations, the applet never works for me. :)

0
votes

Just a note. This thread helped get me on track, so I'd just like to add a helpful note. August 9th 2010.

In Mac OS X 10.5.8 Leopard the version of Java used is controlled by an app in the path Mac HD/Applications/Utilities/Java Preferences

That may have formerly been named Java Settings? The version of Java used for a desktop app does not have to be the same version as used in a browser. I was having a problem launching Portecle (from the command line)- it was reporting a "Bad version number in .class file".

As of August 2010 the Mac OS X 10.5.8 default Java sequence is:

  • J2SE 5.0.......32 bit
  • J2SE 5.0.......32 bit
  • Java SE 6.....64 bit
  • J2SE 5.0.......64 bit
  • J2SE 1.4.2 ...32 bit

Apparently the Java app I was trying to load needed Java SE 6 64 bit, because dragging Java SE 6 to the top of that list instantly fixed the problem, and Portecle was launchable after that from either the command line or with a double-click or portecle.jar. (hooray)

0
votes

Here is your answer

<html>
   <applet code="packagename.HelloWorld.class" height="300" width="300">
</html>

Replace the packagename with your package..