Packages
by David Matuszek

Packages are very simple things. For some reason, however, it is often difficult to get them correct. Here are some short examples of setups that work.

A package is a directory, or folder. All Java files are considered to be in a package; if they are in the same directory, they are in the same (default) package. You don't have to use a package statement if all your files are in one folder. If you do use a package statement, it has to agree with the name of the folder your files are in, and references to the files must start from the folder containing that named folder.

 

Example 1: Applet in default package.

Name of folder: Doesn't matter.

Java file (in this folder) contains:
          No package declaration.
          public class Red extends java.applet.Applet { ... }

HTML file (in this same folder) contains:
           <APPLET code="Red.class" width=350 height=200></APPLET>

 

Example 2: Applet in named package.

Name of folder: GreenPackage

Java file (in folder GreenPackage) contains:
           package GreenPackage;
           public class Green extends java.applet.Applet { ... }

HTML file (in folder GreenPackage) contains:
          <APPLET codebase=.. code="GreenPackage/Green.class" width=350 height=200></APPLET>
or
HTML file (in folder that contains the GreenPackage folder) contains:
           <APPLET codebase=. code="GreenPackage/Green.class" width=350 height=200></APPLET>
or
          <APPLET code="GreenPackage/Green.class" width=350 height=200></APPLET>

Notice that codebase= points to the folder containing the package GreenPackage, not to the folder containing the Green.class file, and code= tells how to find the file from there. If the HTML file is in the folder containing the GreenPackage folder, codebase= is not required.

 

Example 3: Applet in more complicated named package.

Name of folder: Blue, inside a folder named Yellow

Java file (in folder Blue) contains:
           package Yellow.Blue;
           public class Blue extends java.applet.Applet { ... }

HTML file (in folder Blue) contains:
          <APPLET codebase=../.. code="Yellow/Blue/Blue.class" width=350 height=200></APPLET>
or
HTML file (in folder Yellow) contains:
           <APPLET codebase=.. code="Yellow/Blue/Blue.class" width=350 height=200></APPLET>
or
HTML file (in the folder containing folder Yellow) contains:
          <APPLET codebase=. code="Yellow/Blue/Blue.class" width=350 height=200></APPLET>
or
          <APPLET code="Yellow/Blue/Blue.class" width=350 height=200></APPLET>