Package
In this chapter you will learn:
- How to get package for a name
- How to get the package name
- How to get implementation title, vendor and version
- How to get the specification title, vendor and version
- How to get all packages
- How get the annotations for a package
Load package for a name
static Package getPackage(String name)
finds a package by name
public class Main {
public static void main(String[] args) {
Package p = Package.getPackage("javax.swing");
System.out.print("Swing Version: ");
System.out.println(p.getSpecificationVersion());
// ja va2 s . c o m
p = Package.getPackage("java.awt");
System.out.print("AWT Version: ");
System.out.println(p.getSpecificationVersion());
}
}
The output:
Get the package name
String getName()
returns the name of this package.
public class Main {
public static void main(String[] args) {
Package p = Package.getPackage("javax.swing");
//from ja v a2 s.co m
System.out.println(p.getName());
}
}
The output:
Get implementation title, vendor and version
String getImplementationTitle()
returns the title of this package.String getImplementationVendor()
returns the name of the organization, vendor.String getImplementationVersion()
returns the version.
public class Main {
public static void main(String[] args) {
Package p = Package.getPackage("javax.swing");
/* java 2s . co m*/
System.out.println(p.getImplementationTitle());
System.out.println(p.getImplementationVendor());
System.out.println(p.getImplementationVersion());
}
}
The output:
Get the specification title, vendor and version
String getSpecificationTitle()
returns the title of the specification.String getSpecificationVendor()
returns the name of the organization, vendor.String getSpecificationVersion()
returns the version number.
public class Main {
public static void main(String[] args) {
Package p = Package.getPackage("javax.swing");
//j ava 2 s . c o m
System.out.println(p.getSpecificationTitle());
System.out.println(p.getSpecificationVendor());
System.out.println(p.getSpecificationVersion());
}
}
The output:
Get all packages
static Package[] getPackages()
gets all the packages currently known for the ClassLoader.
public class Main {
public static void main(String[] args) {
Package[] ps = Package.getPackages();
/*from j a va 2s. c o m*/
for(Package p:ps){
System.out.println(p);
}
}
}
The output:
Get the package's annotations
<A extends Annotation>A getAnnotation(Class<A> annotationClass)
returns this element's annotation for the specified type if such an annotation is present, else null.Annotation[] getAnnotations()
returns all annotations.Annotation[] getDeclaredAnnotations()
returns all annotations that are directly present on this element.
public class Main {
public static void main(String[] args) {
Package[] ps = Package.getPackages();
/* j av a 2s. c o m*/
for(Package p:ps){
System.out.println(p.getAnnotations().length);
}
}
}
Next chapter...
What you will learn in the next chapter:
- How to get the length of an Array
- How to get value from an Array
- How to set value to an Array
- How to create new Array instance