Java ModuleLayer get modules

Introduction

Listing the Names of Loaded Modules by Class Loader


public class Main {
  public static void main(String[] args) {
    // Get the boot layer
    ModuleLayer layer = ModuleLayer.boot();

    // Print all module's names and their class loader names in the boot layer
    for (Module m : layer.modules()) {
      ClassLoader loader = m.getClassLoader();
      String moduleName = m.getName();
      String loaderName = loader == null ? "bootstrap" : loader.getName();
      System.out.printf("%s: %s%n", loaderName, moduleName);
    }/*from  w  w w.  j  av  a 2s .  c  o  m*/
  }
}



PreviousNext

Related