Module java.base
Package java.lang

Class Package

java.lang.Object
java.lang.Package
All Implemented Interfaces:
AnnotatedElement

public classPackageextendsObjectimplementsAnnotatedElement
Represents metadata about a run-time package associated with a class loader. Metadata includes annotations, versioning, and sealing.

Annotations for the run-time package are read frompackage-info.class at the same code source as classes in the run-time package.

The set of classes that make up the run-time package may implement a particular specification. The specification title, version, and vendor (indicating the owner/maintainer of the specification) can be provided when thePackage is defined. An application can ask if thePackage is compatible with a particular specification version by using thePackage.isCompatibleWith(String) method. In addition, information about the actual classes that make up the run-time package can be provided when thePackage is defined. This information consists of an implementation title, version, and vendor (indicating the supplier of the classes).

APackage may be explicitly defined with theClassLoader.definePackage(String, String, String, String, String, String, String, URL) method. The caller supplies the specification and implementation titles, versions, and vendors. The caller also indicates whether the package issealed. If aPackage is not explicitly defined for a run-time package when a class in that run-time package is defined, then aPackage is automatically defined by the class's defining class loader, as follows.

APackage automatically defined for classes in a named module has the following properties:

  • The name of the package is derived from thebinary names of the classes. Since classes in a named module must be in a named package, the derived name is never empty.
  • The package is sealed with themodule location as the code source, if known.
  • The specification and implementation titles, versions, and vendors are unspecified.
  • Any annotations on the package are read frompackage-info.class as specified above.

APackage automatically defined for classes in an unnamed module has the following properties:

  • The name of the package is either"" (for classes in an unnamed package) or derived from thebinary names of the classes (for classes in a named package).
  • The package is not sealed.
  • The specification and implementation titles, versions, and vendors are unspecified.
  • Any annotations on the package are read frompackage-info.class as specified above.

APackage can be obtained with thePackage.getPackage(String) andClassLoader.getDefinedPackage(String) methods. EveryPackage defined by a class loader can be obtained with thePackage.getPackages() andClassLoader.getDefinedPackages() methods.

Implementation Note:
Thebuiltin class loaders do not explicitly definePackage objects for packages innamed modules. Instead those packages are automatically defined and have no specification and implementation versioning information.
SeeJava Virtual Machine Specification:
5.3 Creation and Loading
Since:
1.2
See Also: