Java.lang.Package类

java.lang.Package.getPackages()方法用于获取调用者的ClassLoader实例当前已知的所有包。这些包对应于通过该 ClassLoader 实例加载或可按名称访问的类。如果调用者的ClassLoader实例是bootstrap ClassLoader实例(在某些实现中可能用null表示),则只会返回bootstrap ClassLoader实例加载的类对应的包。

语法

public static Package[] getPackages()

参数

不需要参数。

返回值

返回调用者 ClassLoader 实例已知的新包数组。如果未知,则返回零长度数组。

异常

无。

示例:

下面的示例显示如何使用 java.lang.Package.getPackages() 方法。

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //获取所有包
    Package[] p = Package.getPackages();

    //打印所有包
    for(Package i: p)
      System.out.println(i);
  }
}

上述代码可能的输出可能是:

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