The Package Objects contains version information about the implementation and specification of Java Package. This information stored in the manifest and distributed among classes. It can be loaded by ClassLoader that loads the classes.
Information can be retrieved like:
- Specification Title
- Version number
- Vendor
- Check compatibility with a particular package.
Methods of Java Package Class
Modifier and Type | Method and Description |
A getAnnotation(Class annotationClass) | Returns this element’s annotation for the specified type if such an annotation is present, else null. |
Annotation[] getAnnotations() | Returns annotations that are present on this element. |
A[] getAnnotationsByType(Class annotationClass) |
Returns annotations that are associated with this element. |
A getDeclaredAnnotation(Class annotationClass) |
Returns this element’s annotation for the specified type if such an annotation is directly present, else null. |
Annotation[] getDeclaredAnnotations() | Returns annotations that are directly present on this element. |
A[] getDeclaredAnnotationsByType(Class annotationClass) | Returns this element’s annotation(s) for the specified type if such annotations are either directly present or indirectly present. |
String getImplementationTitle() | Return the title of this package. |
String getImplementationVendor() | Returns the name of the organization, vendor or company that provided this implementation. |
String getImplementationVersion() | Return the version of this implementation. |
String getName() | Return the name of this package. |
static Package getPackage(String name) | Find a package instance by pacakge name in the callers ClassLoader instance. |
static Package[] getPackages() | Return array of packages currently known for the caller’s ClassLoader instance. |
String getSpecificationTitle() | Return the title of the specification that this package implements. |
String getSpecificationVendor() | Return the name of the organization, vendor, or company that owns and maintains the specification of the classes that implement this package. |
String getSpecificationVersion() | Returns the version number of the specification that this package implements. |
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) | Returns true if an annotation for the specified type is present on this element, else false. |
boolean isCompatibleWith(String desired) | Compare this package’s specification version with a desired compatible version. |
boolean isSealed() | Returns true if this package is sealed. |
boolean isSealed(URL url) | Returns true if this package is sealed with respect to given code source url. |
Package Class Example
public class PackageDetailInfo { public static void main(String args[]) { String packageName="java.lang"; System.out.println("=========Package Detail Info :"+"============"); Package p = Package.getPackage(packageName); System.out.println("Package name: " + p.getName()); System.out.println("Specification Title: " + p.getSpecificationTitle()); System.out.println("Specification Vendor: " + p.getSpecificationVendor()); System.out.println("Specification Version: " + p.getSpecificationVersion()); System.out.println("Implementaion Title: " + p.getImplementationTitle()); System.out.println("Implementation Vendor: " + p.getImplementationVendor()); System.out.println("Implementation Version: " + p.getImplementationVersion()); System.out.println("Is sealed: " + p.isSealed()); } }
Output
=========Package Detail Info :============
Package name: java.lang
Specification Title: Java Platform API Specification
Specification Vendor: Oracle Corporation
Specification Version: 1.8
Implementaion Title: Java Runtime Environment
Implementation Vendor: Oracle Corporation
Implementation Version: 1.8.0_191
Is sealed: false
You must log in to post a comment.