Java.lang.Package.getPackages() Method

Advertisements


Description

The java.lang.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]);
      }
   }
}

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

package sun.util, Java Platform API Specification, version 1.6
package java.util, Java Platform API Specification, version 1.6
package java.net, Java Platform API Specification, version 1.6
package sun.reflect.misc, Java Platform API Specification, version 1.6
package sun.security.provider, Java Platform API Specification, version 1.6
package sun.net.www.protocol.jar, Java Platform API Specification, version 1.6
package sun.security.action, Java Platform API Specification, version 1.6
package java.nio.charset, Java Platform API Specification, version 1.6
package sun.io, Java Platform API Specification, version 1.6
package java.io, Java Platform API Specification, version 1.6
package sun.nio, Java Platform API Specification, version 1.6
package sun.net.www, Java Platform API Specification, version 1.6
package java.lang.ref, Java Platform API Specification, version 1.6
package java.lang, Java Platform API Specification, version 1.6
package sun.misc, Java Platform API Specification, version 1.6
package sun.net.www.protocol.file, Java Platform API Specification, version 1.6
package java.util.concurrent, Java Platform API Specification, version 1.6
package java.util.concurrent.atomic, Java Platform API Specification, version 1.6
package com.tutorialspoint
package java.nio.charset.spi, Java Platform API Specification, version 1.6
package java.nio, Java Platform API Specification, version 1.6
package java.util.concurrent.locks, Java Platform API Specification, version 1.6
package sun.reflect, Java Platform API Specification, version 1.6
package java.security, Java Platform API Specification, version 1.6
package java.lang.reflect, Java Platform API Specification, version 1.6
package sun.nio.cs, Java Platform API Specification, version 1.6
package sun.security.util, Java Platform API Specification, version 1.6
package java.security.cert, Java Platform API Specification, version 1.6
package sun.jkernel, Java Platform API Specification, version 1.6
package sun.net.util, Java Platform API Specification, version 1.6



Advertisements
Advertisements