Java Source Files belonging to the same category or provide similar functionality can be packed into a Java Package. Java Packages are stored in compressed files called jar Files. Inside the package the files can be stored hierachically, therefore it is possible to combine several subdirectories into one package.
- A package provides a unique namespace for the types it contains.
- A package facilitates access protection.
- A package can contain the following kinds of types.
- Classes
- Interfaces
- Enumerated Types
- Annotations
To use a package inside a Java Source File, it must be imported with the import statement, e.g. import java.awt.event.*
This imports all files from the directory java/awt/event
Packages in Java 1.5
- java.lang, java.util: These packages provide basic language functionality like regular expressions, the Reflection API
- java.math: provides mathematical functions
- New I/O: the new I/O framework for Java
- I/O: File Operations
- Networking: Networking Operations, Sockets, DNS lookups,...
- Security: key generation, encryption and decryption
- Java Database Connectivity (JDBC): to access databases
- Java AWT: set of packages for GUI
- Java Swing: native Java GUI components
Creation of Jar Files
In the Java Source Files the package the file shall belong to must be specified with the package keyword, e.g. package java.awt.event
Jar Files are created with the jar command-line utility. The command
jar cf myPackage.jar *.class
compresses all *.class files into the Jar File myPackage.tar.