Packages
How to make, use and compile packages.
Example: the Pumpkin package, containing
two public classes Pumpkin and CarvedPumpkin. It is used in the applet myApplet, which
also needs the public class MouseHandler.
To start with, assume that all the files are in a current directory, referred to from now on as "current".
- Under Linux
- In the files containing the classes that you want to go into the package, add, as first line of
each file:
package Pumpkin;
- Create a directory Pumpkin in the current directory:
mkdir Pumpkin
Move the two files Pumpkin.java and CarvedPumpkin.java into this directory.
- Compile the package from outside the Pumpkin directory, i.e. from the current directory, as follows:
-
javac Pumpkin/*.java
- Now let's take care of the rest of the program, where the package is being used. In the main program
myApplet.java, add the line:
import Pumpkin.*;
- Compile the file from the current directory:
javac myApplet.java
This will compile myApplet.java and MouseHandler.java
- Now you can view the applet as usually, by opening the myApplet.html file where it is invoked.
- Under Windows.
This seems to be trickier, so ask me if you want to do it on your own.
Reading:
Read from Rebecca Hasti's notes,
Lessons, Packages.