java.lang.Package.getPackages()方法範例


java.lang.Package.getPackages() 方法獲取所有目前已知呼叫方的ClassLoader範例的軟體包。這些包對應於通過載入或名稱至ClassLoader範例存取的類。如果呼叫者的ClassLoader範例是引導類載入器的範例,這可能是null,在一些實現來表示,對應於由引導類載入器範例載入的類僅包將被返回。

宣告

以下是java.lang.Package.getPackages()方法的宣告

public static Package[] getPackages()

引數

  • NA

返回值

此方法返回已知呼叫方的ClassLoader範例程式包的新陣列。如果沒有已知的,返回0長度陣列。

異常

  • NA

例子

下面的例子顯示lang.Object.getPackages()方法的使用。

package com.yiibai;

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]);
      }
   }
}

讓我們來編譯和執行上面的程式,這將產生以下結果:

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.yiibai
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