How do I get a simple, Hello World Java applet to work in a browser in Mac OS X?

10,878

Solution 1

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?

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
10,878
Phillip Lemky
Author by

Phillip Lemky

Updated on June 04, 2022

Comments

  • Phillip Lemky
    Phillip Lemky almost 2 years

    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?