Java applet embedded in HTML, help please

Discussion in 'Mac Programming' started by CraigPotz, Dec 7, 2008.

  1. CraigPotz macrumors newbie

    Joined:
    Dec 1, 2008
    #1
    I'm attempting to embed a simple java applet in a HTML document and view it in a browser as my books and internet sources say I should be able. But it doesn't work for me.

    First, I invoke Terminal and enter java -version. I get:
    java version "1.5.0_16"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
    Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)

    So I have Java. This is the 32-bit JDK 5.0_16 version. I also have the 64-bit Java SE 6, but I don't think I need it. Using it instead of JDK 5.0 doesn't seem to make any difference anyway.

    I invoke Dr. Java, and paste the following into the IDE:
    class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.
    }
    }

    I save this to the desktop as "HelloWorld.java".

    The Compiler Output window of Dr. Java says "Compiler ready: JDK 5.0_16 from the runtime class path.", so I know the compiler is ready. The Compiler selection window says JDK 5.0_16. So I click on "Compile", the beachball spins briefly, and a new file "HelloWorld.class" appears on the desktop. Okay so far.

    Then I invoke my copy of Smultron to create this:
    <HTML>
    <HEAD>
    <TITLE>A Simple Java Applet Program</TITLE>
    </HEAD>
    <BODY>
    Now let's see if this works.
    <APPLET CODE="HelloWorld.class">
    </APPLET>
    </BODY>
    </HTML>

    (I've tried this using <OBJECT> tags and object code as well. No difference.)

    I save it as as "HelloWorld.html", and it appears on the desktop just below the java and class files.

    I have five browsers. I enable Java on Firefox, Camino, and Safari (everything else is already enabled on those browsers).

    First I right-click on the html file, then open it in Smultron, and click "Preview" under Tools. Smultron displays the page, but there's a problem. The text is there: "Now let's see if this works." There's a box outline of some sort on the page, obviously the applet trying to work, and it has a red "X" in the upper left corner, much like a busted image. And the java console has opened a window and says in part: "HelloWorld.class is not public or has no public constructor.
    java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of class HelloWorld with modifiers " Actually, there's considerably more diagnostic text, but I'm not sure it's relevant.

    Next I right-click on the html file, and open it in FireFox 3.0.4. I get the text, but no outline or red "X". And the java console opens with the same info as above.

    Opening the file with Camino 1.6.5 gives the same result as FireFox.

    Opening the file with Safari 3.2.1 gives the same result as Smultron, the text, the outline, and the red "X". It also opens up the java console with the same info as Smultron and the others.

    So, it appears that I can copy a java applet, save it, compile it, and embed it in a html document, but when I try to view it in a browser, it doesn't work.

    It seems to me that the java console is trying to tell me something, but I can't interpret the diagnostic message. The word "public" does appear in the applet code, but I've tried the applet both with and without the "public" statement - no difference.

    This should be very simple. I must be doing something wrong. Can anyone tell me what? Thanks in advance.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Is there a problem with just running, from Terminal, from your Desktop directory:
    java HelloWorld
    ?

    Do you NEED to use a browser, or is that just what your examples were using as a "platform" so you don't have to get your hands dirty?

    -Lee
     
  3. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    If you want to write an Applet, you have to extend the Applet class, as in
    Code:
    public class HelloWorld extends Applet {
    ...
    } 
    
    and you won't have a main() method (unless you want to get fancy and run it as an application too). Read up on how to create an applet.
     

Share This Page