Saturday 1 July 2017

Java: Get package specific information

Package is a name space that provides a way to organise interfaces, classes  to avoid naming collisions.

Packages are like Directories. For Example, All the audio files in one directory, video files in other directory and text files in one directory. Similarly, A project can contain several files, so it is always good to organise them in consistent way. Packages used to Organise the classes and interfaces.

How to get package specific information?
‘java.lang.Package’ class is used to get package specific information like package name, title, vendor who implemented this package, specification version, implementation version of the package etc.,

Test.java
public class Test {

 public static void printPackage(Package p) {
  String name = p.getName();
  String title = p.getImplementationTitle();
  String vendorWhoImplentedThisPackage = p.getImplementationVendor();
  String implementationVersion = p.getImplementationVersion();
  
  String titleOfTheSpecification = p.getSpecificationTitle();
  String specVendor = p.getSpecificationVendor();
  String specVersion = p.getSpecificationVersion();
  

  System.out.println("name : " + name);
  System.out.println("title : " + title);
  System.out.println("vendorWhoImplentedThisPackage : " + vendorWhoImplentedThisPackage);
  System.out.println("implementationVersion : " + implementationVersion);
  System.out.println("titleOfTheSpecification : " + titleOfTheSpecification);
  System.out.println("specVendor : " + specVendor);
  System.out.println("specVersion : " + specVersion);
 }

 public static void main(String args[]) {

  Package p = Package.getPackage("java.lang");
  printPackage(p);

 }
}


Output
name : java.lang
title : Java Runtime Environment
vendorWhoImplentedThisPackage : Oracle Corporation
implementationVersion : 1.8.0_131
titleOfTheSpecification : Java Platform API Specification
specVendor : Oracle Corporation
specVersion : 1.8




No comments:

Post a Comment