This article needs attention from an expert on the subject. Please add a reason or a talk parameter to this template to explain the issue with the article. |
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.
Using packages
To use a package inside a Java source file, it is convenient to import the classes from the package with an import
statement. The statement
import java.awt.event.*;
imports all classes from the java.awt.event
package, while
import java.awt.event.ActionEvent;
imports only the ActionEvent
class from the package. After either of these import statements, the ActionEvent
class can be referenced using its simple class name:
ActionEvent myEvent = new ActionEvent();
Classes can also be used directly without an import statement by using the fully-qualified name of the class. For example,
java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();
doesn't require a preceding import statement.
Creation of JAR Files
In Java source files the package the file belongs to is specified with the package
keyword.
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.jar.
Package naming conventions
One of the primary purposes of the package naming conventions is to avoid the possibility of two published packages having the same name by choosing unique package names for packages that are widely distributed. This allows packages to be easily and automatically installed and catalogued.
In general, a package name begins with the top level ___domain name of the organization and then the organization's ___domain and then any subdomains listed in reverse order. The organization can then choose a specific name for their pacakge. Package names should be all lowercase characters whenever possible.
For example, if an organization in Canada called MySoft creates a package to deal with fractions, they might name their package ca.mysoft.fractions
Complete conventions for disambiguating package names and rules for naming packages when the Internet ___domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.
Core packages in J2SE 5.0
java.lang
,java.util
: These package hierarchies provide basic language functionality like types, regular expressions, the Reflection APIjava.math
: provides mathematical functionsjava.io
: file operationsjava.nio
: the New I/O framework for Javajava.net
: Networking Operations, Sockets, DNS lookups, ...java.security
: key generation, encryption and decryptionjava.sql
: Java Database Connectivity (JDBC) to access databasesjava.awt
: basic hierarchy of packages for native GUI componentsjavax.swing
: hierarchy of packages for platform-independent rich GUI components