In Java, a package is a subdirectory of classes.
Just like the .java and .class files
must be named the same as the Java class within them,
so too packages must be named the same as the subdirectory
containing them.
In the following example there is a package called
"utilities"
(a subdirectory called "utilities").
It contains a class called "MiscKit".
MiscKit is intended as a miscellaneous toolkit,
a class with a number of static utility methods.
Back in the current directory (the directory which has the
utilities subdirectory), there is a Java
program PkgDemo.java.
This non-GUI program will import
MiscKit and use its static member
MiscKit.hello(), which simply prints out
a short message to System.out.
C:\Temp>dir
Volume in drive C is WIN98
Volume Serial Number is 3241-11C0
Directory of C:\Temp
. <DIR> 02-25-99 11:27a .
.. <DIR> 02-25-99 11:27a ..
0 file(s) 0 bytes
3 dir(s) 82,574,749 bytes free
C:\Temp>mkdir utilities
C:\Temp>edit PkgDemo.java
C:\Temp>edit utilities\MiscKit.java
C:\Temp>javac PkgDemo.java
C:\Temp>dir
Volume in drive C is WIN98
Volume Serial Number is 3241-11C0
Directory of C:\Temp
. <DIR> 02-25-99 11:27a .
.. <DIR> 02-25-99 11:27a ..
PKGDEM~1 JAV 270 02-25-99 11:35a PkgDemo.java
UTILIT~1 <DIR> 02-25-99 11:31a utilities
PKGDEM~1 CLA 353 02-25-99 11:38a PkgDemo.class
2 file(s) 611 bytes
3 dir(s) 82,575,360 bytes free
C:\Temp>cd utilities
C:\Temp\utilities>dir
Volume in drive C is WIN98
Volume Serial Number is 3241-11C0
Directory of C:\Temp\utilities
. <DIR> 02-25-99 11:31a .
.. <DIR> 02-25-99 11:31a ..
MISCKI~1 JAV 287 02-25-99 11:37a MiscKit.java
MISCKI~1 CLA 453 02-25-99 11:38a MiscKit.class
2 file(s) 750 bytes
2 dir(s) 97,255,424 bytes free
C:\Temp\utilities>cd ..
C:\Temp>java PkgDemo
Hello, World!
C:\Temp>
| Sample DOS session showing the
contents of various directories
You can use the DOS command md
or mkdir to create a new directory.
Notice how MistKit.java was automatically compiled
when PkgDemo.java was.
javac will automatically re-compile
MiscKit.java if it is newer than
MiscKit.class, or if MiscKit.class
is missing.
|
// Demonstration of Java packages.
// (C) 1999 by Wayne Pollock, Tampa FL USA.
// All Rights Reserved.
import utilities.MiscKit; // or: import utilities.*;
public class PkgDemo
{
public static void main ( String [] args )
{
MiscKit.hello();
}
}
| Download PkgDemo.java |
| | |
// Demonstrates Java packages.
// (C) 1999 by Wayne Pollock, Tampa FL USA.
// All Rights Reserved.
package utilities;
public class MiscKit
{
// A static method which prints "hello":
public static void hello ()
{
System.out.println( "Hello, World!" );
}
}
| Download MiscKit.java (Put in a subdirectory called "utilities".) |
This is a two step process.
First you must put your package into a jar
(Java ARchive) file.
Then you must copy that jar file into the correct extensions directory.
C:\Temp>jar -cvf utilities.jar utilities
added manifest
adding: utilities/(in = 0) (out= 0)(stored 0%)
adding: utilities/MiscKit.class(in = 407) (out= 290)(deflated 28%)
C:\Temp>copy utilities.jar "\Program Files\Java\jre1.6.0\lib\ext"
1 file(s) copied.
C:\Temp>copy utilities.jar C:\java\jre\lib\ext
1 file(s) copied.
C:\Temp>