Home >Java >javaTutorial >In Java 9, how can we display all modules that have classloaders?

In Java 9, how can we display all modules that have classloaders?

PHPz
PHPzforward
2023-08-19 20:09:081380browse

在Java 9中,我们如何显示所有具有类加载器的模块呢?

Before Java 9, the extension and the application class loader are an instance of the java.net.URLClassLoader class. In Java 9, the classification of class loaders has changed, instead of an external class loader, we have the Platform class loader. The purpose of using Platform class loader is that classes loaded by the bootstrap class loader have all permissions by default.

In the below example, we can display all modules with classloaders.

Example

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>

The above is the detailed content of In Java 9, how can we display all modules that have classloaders?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete