Java Package - getPackages() Method
The java.lang.Package.getPackages() method is used to get all the packages currently known for the caller's ClassLoader instance. Those packages correspond to classes loaded via or accessible by name to that ClassLoader instance. If the caller's ClassLoader instance is the bootstrap ClassLoader instance, which may be represented by null in some implementations, only packages corresponding to classes loaded by the bootstrap ClassLoader instance will be returned.
Syntax
public static Package[] getPackages()
Parameters
No parameter is required.
Return Value
Returns a new array of packages known to the callers ClassLoader instance. An zero length array is returned if none are known.
Exception
NA.
Example:
The example below shows how to use java.lang.Package.getPackages() method.
import java.lang.*; public class MyClass { public static void main(String[] args) { //get all packages Package[] p = Package.getPackages(); //print all packages for(Package i: p) System.out.println(i); } }
The possible output of the above code could be:
package sun.reflect, Java Platform API Specification, version 1.8 package java.util, Java Platform API Specification, version 1.8 package sun.reflect.annotation, Java Platform API Specification, version 1.8 package java.lang.annotation, Java Platform API Specification, version 1.8 package java.nio, Java Platform API Specification, version 1.8 package sun.nio, Java Platform API Specification, version 1.8 package java.security.cert, Java Platform API Specification, version 1.8 package java.util.zip, Java Platform API Specification, version 1.8 package sun.launcher, Java Platform API Specification, version 1.8 package sun.security.action, Java Platform API Specification, version 1.8 package java.nio.file, Java Platform API Specification, version 1.8 package java.nio.charset, Java Platform API Specification, version 1.8 package sun.net.www, Java Platform API Specification, version 1.8 package java.lang.ref, Java Platform API Specification, version 1.8 package java.net, Java Platform API Specification, version 1.8 package sun.net.www.protocol.file, Java Platform API Specification, version 1.8 package java.lang.invoke, Java Platform API Specification, version 1.8 package sun.util.locale, Java Platform API Specification, version 1.8 package sun.reflect.generics.repository, Java Platform API Specification, version 1.8 package sun.misc, Java Platform API Specification, version 1.8 package java.lang.reflect, Java Platform API Specification, version 1.8 package sun.net.util, Java Platform API Specification, version 1.8 package java.security, Java Platform API Specification, version 1.8 package sun.net.www.protocol.jar, Java Platform API Specification, version 1.8 package java.util.concurrent, Java Platform API Specification, version 1.8 package java.util.concurrent.atomic, Java Platform API Specification, version 1.8 package java.util.concurrent.locks, Java Platform API Specification, version 1.8 package sun.util, Java Platform API Specification, version 1.8 package java.lang, Java Platform API Specification, version 1.8 package java.io, Java Platform API Specification, version 1.8 package sun.reflect.misc, Java Platform API Specification, version 1.8 package sun.nio.ch, Java Platform API Specification, version 1.8 package java.util.jar, Java Platform API Specification, version 1.8 package sun.nio.cs, Java Platform API Specification, version 1.8 package java.util.function, Java Platform API Specification, version 1.8 package java.nio.charset.spi, Java Platform API Specification, version 1.8 package sun.security.util, Java Platform API Specification, version 1.8
❮ Java.lang - Package