Package getPackages() method in Java with Examples

The Java.lang.Package has getPackages() function. The caller's class loader defines the Packages, which can be obtained via the package class. An array of Package objects is the method's return value for the packages.

Syntax:

Parameter: There are no parameters accepted by this method.

Return Value: The packages are returned by this method as an array of Package objects.

Example 1:

The code defines the Java class GetPackagesExample1, whose main function uses the Package.getPackages() method to retrieve all of the packages that are currently available to the Java virtual system. These packages are kept in an array pack. Next, when iterating through this array, the code prints each package separately. This shows how to use Java's Package class to list every package that is accessible in the current runtime environment.

Implementation:

FileName: GetPackagesExample1.java

Output:

 
package sun.security.util
package jdk.internal.util.random
package jdk.internal.loader
package java.util.random
package java.time.format
package java.security.cert
package sun.invoke.empty
package sun.util.resources
package sun.util
package sun.nio
package jdk.internal.misc
package sun.nio.cs
package java.nio.file.spi
package java.io
package jdk.internal.reflect
package jdk.internal.jimage.decompressor
package sun.util.locale
package java.time.temporal
package sun.text.spi
package sun.text.resources.cldr
package java.nio.charset.spi
package sun.util.resources.cldr
package sun.launcher
package jdk.internal.ref
package sun.net.www.protocol.jar
package java.util.jar
package java.text.spi
package java.lang.ref
package sun.nio.ch
package sun.security.action
package sun.util.logging
package jdk.internal.util
package sun.util.calendar
package java.text
package sun.net.util
package jdk.internal.logger
package java.util.concurrent
package jdk.internal.vm
package java.time.chrono
package jdk.internal.org.objectweb.asm
package jdk.internal.math
package java.util.concurrent.atomic
package jdk.internal.access
package java.lang
package java.util.zip
package java.util.function
package java.net.spi
package jdk.internal.vm.vector
package java.math
package java.util.concurrent.locks
package java.util.regex
package sun.net.www.protocol.file
package java.security
package jdk.internal.foreign.abi
package java.lang.reflect
package sun.net.www
package java.nio.file
package sun.util.logging.internal
package sun.invoke.util
package java.util.spi
package sun.reflect.annotation
package jdk.internal.module
package java.net
package sun.util.locale.provider
package java.nio.charset
package java.nio.file.attribute
package java.lang.module
package java.lang.annotation
package java.util.logging
package java.time.zone
package sun.nio.fs
package java.time
package jdk.internal.jimage
package java.lang.constant
package jdk.internal.jmod
package sun.util.cldr
package java.util
package jdk.internal.perf
package java.util.stream
package java.nio
package java.lang.invoke
package sun.util.spi
package