Search code examples
javahtmlapplet

browser doesn't show java applet


I'm just starting with java applets and found this website that gives a tutorial. I entered all the code but the applet didn't show up in my browser: HelloWorld.java

import java.applet.*;
import java.awt.*;

public class HelloWorld extends Applet
{
    public void paint (Graphics g)
    {
    g.drawString("Hello World!", 300, 300);
    }
}

And this is my HTML file

<HTML>
<HEAD>
<TITLE>Hello World Applet</TITLE>
</HEAD>
<BODY>
<H1>Hello World Applet</H1>
<applet code="HelloWorld.class" WIDTH=300 HEIGHT=300></applet>
</BODY>
</HTML>

I have compiled the java file (HelloWorld.class) and saved all the files on my desktop. I'm using the latest version of safari on mac osx mountain lion. I tried it in google chrome and it said to install a plugin and i did but nothing more happened?


Solution

  • (Just Note: These things take place in windows and a few in Ubuntu. Just check the following if any of the following applies to your problem)Try the following things:
    1st- In HTML file, change the body tag to:

    <body bgcolor = red>
    

    SO that you know your applet is running on the browser if you find a white rectangle of the dimensions you mentioned but the applet is not running as expected because of some problem with the browser/jre.

    2nd- Browsers that we use are generally 32 bit browsers. Do check if you have the 32 bit version of java installed in case your browser is a 32 bit-one.

    3rd- To check whether your applet is actually created, try running the html file using appletviewer using the terminal.

    run the following either in cmd/terminal whichever you use:

    appletviewer filename.html

    or if you are using eclipse/netbeans, they usually have a appletViewer integrated along with the IDE so they run the appletviewer directly.