Home » Java programming language

Java Package getPackages() method with example

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

Package Class getPackages() method

  • getPackages() method is available in java.lang package.
  • getPackages() method is used to return all the packages recently for the callers ClassLoader instance when the caller ClassLoader instance is the bootstrap ClassLoader instance which may be denoted by null in some deployments so the only packages similar loaded by bootstrap loader instance will be returned.
  • getPackages() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • getPackages() method does not throw an exception at the time of return packages.

Syntax:

    public static Package[] getPackages();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Package[], it returns san array of packages for the caller's ClassLoader instance otherwise it returns 0 when no package exists.

Example:

// Java program to demonstrate the example 
// of Package[] getPackages() of Package method

public class GetPackages {
    public static void main(String[] args) {
        // Get Packages by using getPackages() method
        Package[] pkg = Package.getPackages();

        // Display name of the packages 
        System.out.print("Package Name: ");
        for (int i = 0; i < pkg.length; ++i)
            System.out.print(pkg[i].getName());
    }
}

Output

Package Name: java.util.zipjava.lang.reflectjava.util.concurrentjdk.internal.org.objectweb.asmsun.security.actionjdk.internal.reflectsun.net.www.protocol.jrtjava.nio.filejava.nio.file.attributesun.security.utiljava.netjava.securityjdk.jfr.internaljava.util.concurrent.atomicjava.util.concurrent.locksjava.nio.file.spijdk.internal.miscjava.lang.refsun.launcherjdk.internal.loadersun.net.wwwjava.utilsun.niosun.invoke.utiljava.lang.invokejdk.internal.refsun.net.www.protocol.jarsun.util.localesun.nio.fssun.reflect.annotationjava.util.streamjava.lang.modulesun.net.utiljdk.internal.utilsun.nio.csjava.util.functionjava.niojava.util.jarjava.iojava.security.certjava.langsun.net.www.protocol.filejava.nio.charset.spijdk.internal.modulejava.lang.annotationjava.nio.charset



Comments and Discussions!

Load comments ↻






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