在 Java 9 之前,擴充功能和應用程式類別載入器是 java.net.URLClassLoader 類別的實例。在Java 9中,類別載入器的分類發生了變化,我們不再使用外部類別載入器,而是使用Platform類別載入器。使用 Platform 類別載入器的目的是 bootstrap 類別載入器載入的類別預設擁有所有權限。
在下面的範例中,我們可以使用類別載入器顯示所有模組。範例
import static java.util.Objects.isNull; public class Java9ClassLoaderTest { public static void main(String args[]) { <strong> ModuleLayer </strong>layer = <strong>ModuleLayer.boot()</strong>; layer.modules().forEach(module -> { <strong> ClassLoader </strong>classLoader = module.<strong>getClassLoader()</strong>; String classLoaderName = <strong>isNull</strong>(classLoader) ? "bootstrap" : classLoader.<strong>getName()</strong>; System.out.println(classLoaderName + ": " + module.<strong>getName()</strong>); }); } }
<strong>app: jdk.internal.ed platform: jdk.scripting.nashorn platform: java.compiler platform: java.security.jgss platform: jdk.plugin.dom bootstrap: jdk.net bootstrap: java.management app: jdk.jdeps platform: javafx.base app: jdk.editpad bootstrap: java.security.sasl platform: java.sql.rowset bootstrap: java.datatransfer app: jdk.compiler platform: java.scripting platform: jdk.naming.dns platform: jdk.crypto.mscapi bootstrap: java.xml platform: jdk.localedata platform: javafx.swing bootstrap: jdk.unsupported platform: jdk.plugin bootstrap: jdk.snmp bootstrap: jdk.naming.rmi app: jdk.internal.le platform: javafx.web platform: jdk.dynalink platform: jdk.accessibility bootstrap: java.rmi platform: jdk.jsobject platform: java.sql app: jdk.attach bootstrap: jdk.management.cmm platform: jdk.security.auth platform: java.xml.crypto bootstrap: jdk.jfr bootstrap: jdk.management.jfr platform: jdk.security.jgss app: jdk.packager.services bootstrap: java.base app: jdk.internal.opt bootstrap: java.prefs bootstrap: java.management.rmi platform: jdk.zipfs platform: java.jnlp app: jdk.packager platform: oracle.net app: jdk.jlink bootstrap: jdk.management.agent bootstrap: java.logging app: jdk.jdi app: jdk.jstatd platform: java.se app: jdk.javadoc platform: jdk.charsets platform: java.smartcardio app: jdk.jconsole bootstrap: jdk.sctp platform: javafx.graphics bootstrap: jdk.management.resource app: jdk.jdwp.agent app: jdk.internal.jvmstat platform: javafx.controls platform: jdk.crypto.cryptoki platform: javafx.media bootstrap: jdk.httpserver platform: oracle.desktop bootstrap: java.naming app: jdk.jshell app: jdk.jartool platform: jdk.deploy bootstrap: java.desktop platform: javafx.fxml bootstrap: java.instrument platform: jdk.crypto.ec bootstrap: jdk.management platform: jdk.xml.dom platform: jdk.javaws</strong>
以上是在Java 9中,我們如何顯示所有具有類別載入器的模組呢?的詳細內容。更多資訊請關注PHP中文網其他相關文章!