Java Package getPackages() Method



Description

The Java Package getPackages() method gets 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.

Declaration

Following is the declaration for java.lang.Package.getPackages() method

public static Package[] getPackages()

Parameters

NA

Return Value

This method 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 following example shows the usage of lang.Object.getPackages() method.

package com.tutorialspoint;

public class PackageDemo {

   public static void main(String[] args) {

      // get all the packages
      Package[] pack = Package.getPackages();

      // print all packages, one by one
      for (int i = 0; i < pack.length; i++) {
         System.out.println(pack[i]);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

package sun.security.action
package jdk.internal.access
package java.nio.charset
package jdk.internal.vm.vector
package java.nio
package sun.net.www.protocol.file
package java.security
package jdk.internal.vm
package sun.net.www.protocol.jar
package jdk.internal.loader
package java.util.concurrent.atomic
package java.lang.ref
package java.util
package jdk.internal.foreign.abi
package sun.util
package java.io
package sun.io
package sun.security.util
package jdk.internal.reflect
package jdk.internal.misc
package java.lang.module
package java.net
package java.util.concurrent.locks
package java.lang.constant
package java.lang.annotation
package sun.invoke.empty
package sun.launcher
package jdk.internal.org.objectweb.asm
package jdk.internal.ref
package sun.nio
package java.lang.invoke
package sun.nio.cs
package sun.net.www
package jdk.internal.util
package java.lang.reflect
package java.security.cert
package sun.reflect.annotation
package jdk.internal.module
package java.nio.charset.spi
package java.util.jar
package sun.invoke.util
package java.util.function
package java.util.stream
package java.util.concurrent
package java.lang
package sun.net.util
package com.tutorialspoint
java_lang_package.htm
Advertisements