How can we display all modules with classloaders in Java 9?

JavaObject Oriented ProgrammingProgramming

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[]) {
      ModuleLayer layer = ModuleLayer.boot();
      layer.modules().forEach(module -> {
         ClassLoader classLoader = module.getClassLoader();
         String classLoaderName = isNull(classLoader) ? "bootstrap" : classLoader.getName();
         System.out.println(classLoaderName + ": " + module.getName());
      });
   }
}

Output

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
raja
Published on 28-Apr-2020 22:06:50
Advertisements