當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.lang.Package.getPackages()用法及代碼示例



描述

這個java.lang.Package.getPackages()方法獲取調用者的 ClassLoader 實例當前已知的所有包。這些包對應於通過該 ClassLoader 實例的名稱加載或可訪問的類。如果調用者的ClassLoader實例是bootstrap ClassLoader實例,在某些實現中可能用null表示,則隻返回bootstrap ClassLoader實例加載的類對應的包。

聲明

以下是聲明java.lang.Package.getPackages()方法

public static Package[] getPackages()

參數

NA

返回值

此方法返回調用者 ClassLoader 實例已知的包的新數組。如果不知道,則返回零長度數組。

異常

NA

示例

下麵的例子展示了 lang.Object.getPackages() 方法的用法。

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

讓我們編譯並運行上麵的程序,這將產生以下結果——

package sun.util, Java Platform API Specification, version 1.8
package java.util, Java Platform API Specification, version 1.8
package java.net, Java Platform API Specification, version 1.8
package sun.reflect.misc, Java Platform API Specification, version 1.8
package sun.security.provider, Java Platform API Specification, version 1.8
package sun.net.www.protocol.jar, Java Platform API Specification, version 1.8
package sun.security.action, Java Platform API Specification, version 1.8
package java.nio.charset, Java Platform API Specification, version 1.8
package sun.io, Java Platform API Specification, version 1.8
package java.io, Java Platform API Specification, version 1.8
package sun.nio, 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.lang, Java Platform API Specification, version 1.8
package sun.misc, Java Platform API Specification, version 1.8
package sun.net.www.protocol.file, 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 com.tutorialspoint
package java.nio.charset.spi, Java Platform API Specification, version 1.8
package java.nio, Java Platform API Specification, version 1.8
package java.util.concurrent.locks, Java Platform API Specification, version 1.8
package sun.reflect, Java Platform API Specification, version 1.8
package java.security, Java Platform API Specification, version 1.8
package java.lang.reflect, Java Platform API Specification, version 1.8
package sun.nio.cs, Java Platform API Specification, version 1.8
package sun.security.util, Java Platform API Specification, version 1.8
package java.security.cert, Java Platform API Specification, version 1.8
package sun.jkernel, Java Platform API Specification, version 1.8
package sun.net.util, Java Platform API Specification, version 1.8

相關用法


注:本文由純淨天空篩選整理自 Java.lang.Package.getPackages() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。