Home » Java programming language

Java ClassLoader getPackage() method with example

ClassLoader Class getPackage() method: Here, we are going to learn about the getPackage() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 26, 2019

ClassLoader Class getPackage() method

  • getPackage() method is available in java.lang package.
  • getPackage() method is used to return the package that has been defined in ClassLoader or the Package has been defined in ClassLoader ancestors.
  • getPackage() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getPackage() method does not throw an exception at the time of returning an object of Package type.

Syntax:

    protected Package getPackage(String pack_name);

Parameter(s):

  • String pack_name – represents the name of the Package.

Return value:

The return type of this method is Package, it returns the following values based on the given cases,

  • It returns the Package of the class which is loaded by the class loader of this Class.
  • It returns null, when no package is loaded by the class loader of this Class.

Example:

// Java program to demonstrate the example 
// of Package getPackage(String pack_name) method of 
// ClassLoader

public class GetPackageOfClass extends ClassLoader {
    void getPackage() throws ClassNotFoundException {
        // It return the package
        Package pack = super.getPackage("java.lang");

        // Display Package Name
        System.out.print("Package Name: ");
        System.out.println(pack);
    }

    public static void main(String[] args) throws Exception {
        GetPackageOfClass cp = new GetPackageOfClass();
        cp.getPackage();
    }
}

Output

Package Name: package java.lang



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.