如何将一个JAR包添加到Java应用程序的Boot Classpath中?
1. 在启动脚本中使用-bootstrap或-Xbootclasspath选项
这两个选项的使用方式如下:
-
-bootstrap
选项:java -bootstrap /path/to/your.jar -cp /path/to/your/app.jar YourMainClass
-
-Xbootclasspath
选项:java -Xbootclasspath/a:/path/to/your.jar -cp /path/to/your/app.jar YourMainClass
请注意,-bootstrap
选项在某些Java版本中可能不受支持,而-Xbootclasspath
选项通常在大多数Java虚拟机中可用。
2. 通过manifest file(jar包META-INF/MANIFEST.MF目录下)中的Boot-Class-Path属性实现
Maven项目中,您可以通过使用maven-jar-plugin
插件来配置JAR文件的Manifest属性。下面是如何配置Manifest属性的一般步骤:
-
打开项目的
pom.xml
文件。 -
在
build
元素下,添加plugins
元素,如果尚不存在的话。然后在plugins
元素内部配置maven-jar-plugin
插件。示例如下:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.2</version> <configuration> <archive> <manifestEntries> <Premain-Class>com.br.prometheus.SPSExporter</Premain-Class> <Boot-Class-Path>${project.build.finalName}.jar</Boot-Class-Path> <Can-Redefine-Classes>false</Can-Redefine-Classes> <Can-Retransform-Classes>true</Can-Retransform-Classes> <Can-Set-Native-Method-Prefix>false</Can-Set-Native-Method-Prefix> </manifestEntries> </archive> </configuration> </plugin> <!-- 其他插件配置 --> </plugins> </build>
在上面的示例中,我们配置了
maven-jar-plugin
插件,并在<manifestEntries>
元素下添加了一些属性。其中:Manifest Attributes
The following manifest attributes are defined for an agent JAR file:
-
Premain-Class
When an agent is specified at JVM launch time this attribute specifies the agent class. That is, the class containing the
premain
method. When an agent is specified at JVM launch time this attribute is required. If the attribute is not present the JVM will abort. Note: this is a class name, not a file name or path. -
Agent-Class
If an implementation supports a mechanism to start agents sometime after the VM has started then this attribute specifies the agent class. That is, the class containing the
agentmain
method. This attribute is required, if it is not present the agent will not be started. Note: this is a class name, not a file name or path. -
Boot-Class-Path
A list of paths to be searched by the bootstrap class loader. Paths represent directories or libraries (commonly referred to as JAR or zip libraries on many platforms). These paths are searched by the bootstrap class loader after the platform specific mechanisms of locating a class have failed. Paths are searched in the order listed. Paths in the list are separated by one or more spaces. A path takes the syntax of the path component of a hierarchical URI. The path is absolute if it begins with a slash character ('/'), otherwise it is relative. A relative path is resolved against the absolute path of the agent JAR file. Malformed and non-existent paths are ignored. When an agent is started sometime after the VM has started then paths that do not represent a JAR file are ignored. This attribute is optional.
-
Can-Redefine-Classes
Boolean (
true
orfalse
, case irrelevant). Is the ability to redefine classes needed by this agent. Values other thantrue
are consideredfalse
. This attribute is optional, the default isfalse
. -
Can-Retransform-Classes
Boolean (
true
orfalse
, case irrelevant). Is the ability to retransform classes needed by this agent. Values other thantrue
are consideredfalse
. This attribute is optional, the default isfalse
. -
Can-Set-Native-Method-Prefix
Boolean (
true
orfalse
, case irrelevant). Is the ability to set native method prefix needed by this agent. Values other thantrue
are consideredfalse
. This attribute is optional, the default isfalse
.
An agent JAR file may have both the
Premain-Class
andAgent-Class
attributes present in the manifest. When the agent is started on the command-line using the-javaagent
option then thePremain-Class
attribute specifies the name of the agent class and theAgent-Class
attribute is ignored. Similarly, if the agent is started sometime after the VM has started, then theAgent-Class
attribute specifies the name of the agent class (the value ofPremain-Class
attribute is ignored). -
-
保存
pom.xml
文件。 -
使用Maven命令构建项目。您可以运行以下命令来生成包含指定Manifest属性的JAR文件:
mvn clean package
这将生成一个JAR文件,其中包含了配置的Manifest属性。
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: mingming.chen Build-Jdk: 1.8.0_211 Boot-Class-Path: sps_exporter.jar Can-Redefine-Classes: false Can-Retransform-Classes: true Can-Set-Native-Method-Prefix: false Premain-Class: com.br.prometheus.SPSExporter
通过这种方式,您可以方便地配置JAR文件的Manifest属性,包括类路径、主类和其他自定义属性。请根据您的项目需求进行相应的配置。
通过以上方式java agent可以字节码修改jdk中的类