https://poi.apache.org/components/index.html Apache POI 项目是开发基于 Microsoft 的 OLE 2 复合文档格式的文件格式的纯 Java 端口的主项目。 Microsoft Office Documents 以及使用 MFC 属性集序列化其文档对象的程序都使用 OLE 2 复合文档格式。 Apache POI 也是开发基于Office Open XML (ooxml) 的文件格式的纯Java 端口的主项目。 OOXML 是 ECMA/ISO 标准化工作的一部分。该文档相当大，但您通常可以毫不费力地找到所需的内容！ ECMA-376 标准在这里，并且也在 Microsoft OSP 之下。
In this tutorial, we'll analyze the technique to list all the classes loaded by a specific class loader in Java, using the Java Instrumentation API. We'll also see how to create and load a Java agent to acquire an Instrumentation instance and invoke the required methods to accomplish our task.
2. Class Loaders in Java
The class loaders are an integral part of the JRE (Java Runtime Environment). Their job is to dynamically load classes into the Java Virtual Machine. In other words, they load classes into memory on-demand when required by the application. The article on Java class loaders talks about their different types and provides a detailed understanding of how they work.
3. Using the Instrumentation API
The Instrumentation interface provides the getInitiatedClasses(Classloader loader) method that can be invoked toreturn an array comprising all the classes loaded by the particular loader. Let's see how this works.
First, we need to create and load an agent to acquire an instance of the Instrumentation interface. A Java agent is a tool to instrument programs running on the JVM (Java Virtual Machine).
In other words, it can add or modify the bytecode of methods for the purpose of gathering data. We'll require an agent to get a handle on the Instrumentation instance and invoke the required method.
There are multiple ways to create and load an agent. In this tutorial, we'll use the static loading approach using the premain method and the -javaagent option.
3.1. Creating a Java Agent
To create a Java agent, we need to define the premain method to which the Instrumentation instance will be passed on agent load. Let's now create the ListLoadedClassesAgent class:
The complete list of manifest attributes for an agent JAR file is available on the official documentation of the java.lang.instrument package.
3.4. Loading the Agent and Running the Application
Let's now load the agent and run the application. First, we need the agent JAR file with a manifest file containing the Premain-Class information. Additionally, we need the application JAR file with a manifest file containing the Main-Class information. The Launcher class containing the main method will start our application. Then we'll be able to print the classes loaded by different types of class loaders:
In this tutorial, we learned about the technique to list all the classes loaded in a specific class loader.
First, we created the Java Agent. After that, we defined the method to list the loaded classes using the Java Instrumentation API. Finally, we created the agent manifest files, loaded the agent, and ran our application.
As always, the complete source code of the example can be found over on GitHub.