Home »
Java programming language
Java ClassLoader getPackages() method with example
ClassLoader Class getPackages() method: Here, we are going to learn about the getPackages() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 26, 2019
ClassLoader Class getPackages() method
- getPackages() method is available in java.lang package.
- getPackages() method is used to return an array of Packages which is defined by this class loader or the Packages which is defined by this class loader ancestors.
- getPackages() 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.
- getPackages() method does not throw an exception at the time of returning an object of Package.
Syntax:
protected Package[] getPackages();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is Package[], it returns an array of package defined by this ClassLoader.
Example:
// Java program to demonstrate the example
// of Package[] getPackages() method of ClassLoader
public class GetPackagesOfClass extends ClassLoader {
void getPackags() throws ClassNotFoundException {
// It return the packages
Package[] pack = super.getPackages();
// Display Package Name
System.out.print("Package List: ");
for (int i = 0; i < pack.length; ++i)
System.out.println(pack[i].getName());
}
public static void main(String[] args) throws Exception {
GetPackagesOfClass gp = new GetPackagesOfClass();
gp.getPackags();
}
}
Output
Package List: java.nio.file
java.lang.reflect
sun.security.action
java.util.concurrent
sun.net.www.protocol.jrt
java.util.zip
jdk.jfr.internal
sun.nio
java.net
java.security
java.lang.module
sun.reflect.annotation
sun.launcher
java.util.concurrent.locks
jdk.internal.misc
jdk.internal.org.objectweb.asm
java.lang.ref
jdk.internal.loader
sun.security.util
java.util
java.lang.invoke
sun.net.www
java.util.concurrent.atomic
jdk.internal.ref
sun.invoke.util
java.nio
sun.net.www.protocol.jar
java.util.jar
java.nio.file.attribute
java.util.function
java.nio.charset
java.io
java.util.stream
java.security.cert
java.lang
jdk.internal.util
java.nio.file.spi
sun.util.locale
jdk.internal.reflect
sun.net.util
sun.nio.cs
jdk.internal.module
java.lang.annotation
sun.net.www.protocol.file
sun.nio.fs
java.nio.charset.spi