User1000547
User1000547

Reputation: 4301

Applet code tags and class files

I just started writing Applets and I had some questions about the HTML applet tags with packages, and class files.

I'll start with packages. I've been using Netbeans to code my applets, which includes a package automatically. The applet runs fine in the Netbeans applet viewer, and the HTML is displaying everything it should except the applet - there's just a white box that says "Error." When I re-compiled using Eclipse without the package, the HTML display the applet fine. I tried everything I could think of, but I could not for the life of me get the applet to work with the package.

<applet code="myClass.class"width="500"height="500">

<applet code="myPackage.myCLass.class"width="500"height="500">

<applet code="myPackage\myClass.class"width="500"height="500">

Nothing worked. So, my first question is this: what should my HTML applet tag path say in order to get to an applet which is contained in a package? Honestly, all of my google searches yielded results that I didn't really understand, as I'm not 100% sure what a package is/does.

Which brings us to my next question: class files. The applet tag points a a class file, i.e., a compiled source file. Compiling a .java file creates a .class file, simple enough. But how does one get from an applet java file to an applet class file? Attempting to compile via the command line or any IDE fails, because there's no main method. So far, the only way that I've been able to get a class file for an applet is to compile the code either using Netbean's applet viewer or "Run as an applet" in Eclipse. Is there another way to obtain the class file? Or do I have to compile using an applet emulator (for lack of a better word)?

Upvotes: 2

Views: 5131

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168815

<applet code="myPackage.myCLass.class"width="500"height="500">

As to the applet element for a myClass applet class in the myPackage package.

1) An applet element should have spaces between the attributes (I'm not sure if that is part of the W3C recommendation, but it just looks weird).

<applet code="myPackage.myCLass.class" width="500" height="500">

2) The code attribute should be the fully qualified name of the class, which means:

<applet code="myPackage.myCLass" width="500" height="500">

3) With no archives and no codebase specified, the JRE would look for the class in the myPackage sub-directory of the directory from which the HTML is loaded. E.G. if the HTML is called applet.html and is located at:

The class would need to be located at:

Once you think you have the applet class correct, try fetching it using the address in the browser address bar. If the class is not offered for download, the JRE won't be able to load it either.

Upvotes: 4

Related Questions