如何将一个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-ClassWhen an agent is specified at JVM launch time this attribute specifies the agent class. That is, the class containing the
premainmethod. 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-ClassIf 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
agentmainmethod. 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-PathA 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-ClassesBoolean (
trueorfalse, case irrelevant). Is the ability to redefine classes needed by this agent. Values other thantrueare consideredfalse. This attribute is optional, the default isfalse.Can-Retransform-ClassesBoolean (
trueorfalse, case irrelevant). Is the ability to retransform classes needed by this agent. Values other thantrueare consideredfalse. This attribute is optional, the default isfalse.Can-Set-Native-Method-PrefixBoolean (
trueorfalse, case irrelevant). Is the ability to set native method prefix needed by this agent. Values other thantrueare consideredfalse. This attribute is optional, the default isfalse.
An agent JAR file may have both the
Premain-ClassandAgent-Classattributes present in the manifest. When the agent is started on the command-line using the-javaagentoption then thePremain-Classattribute specifies the name of the agent class and theAgent-Classattribute is ignored. Similarly, if the agent is started sometime after the VM has started, then theAgent-Classattribute specifies the name of the agent class (the value ofPremain-Classattribute 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中的类
如何将一个JAR包添加到Java应用程序的Boot Classpath中?的更多相关文章
- build path libraries java基础--Jar包添加到build path方式说明--01
摘自: http://blog.csdn.net/haolongabc/article/details/7007701 java基础--Jar包添加到build path方式说明--01 前言:这段短 ...
- 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)
原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...
- 将一个jar包放到linux下定时执行
将一个jar包放到linux下定时执行 1.在dbtodb文件夹下新建一个dbtodb.sh,脚本内容为: #!/bin/bash cd /usr/dbtodb/ java -jar dbtodb.j ...
- IDEA中将WEB-INF\lib下的Jar包添加到项目中
打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S]左侧选中Modules,在Dependecies中,点击右侧“+”号,选择JARS or directorie ...
- 从jar包还原出java源码(项目文件)
原文转载至:https://blog.csdn.net/mxmxz/article/details/73043156 上周接到个新任务,一个遗留的接口工程需要改造,然而根据前任开发留下的文档看,这个工 ...
- maven 将jar包添加到本地仓库
maven 如何将jar包添加到本地仓库 CreateTime--2018年4月19日12:50:50 Author:Marydon 情景描述:当项目所需的jar包,maven中央仓库中没有该j ...
- maven 如何将自己的jar包添加到本地仓库
1 准备一个需要添加到本地仓库的jar包 我这里准备了一个名为mail.jar 的jar包,放到E:\Install Files目录下面 2 下面演示如何将准备的jar包添加到本地仓库 1 语法 mv ...
- IDEA:将WEB-INF\lib下的Jar包添加到项目中
打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S] 左侧选中Modules,在Dependecies中,点击右侧"+"号,选择JARS or ...
- 本地jar包添加至Maven仓库
Maven命令将本地的jar包方放到maven仓库中 //自定义本地的jar包在pom文件的参数 <dependency> <groupId>com.eee</group ...
- springboot使用jar包方式启动,找不到resources目录中的配置文件(运行时)FileNotFoundException
将springboot项目打包成jar包,使用 java -jar jar包进行启动,富文本框使用ckeditor+ckfinder: 因为ckfinder自定义配置文件了,上传图片时出现了异常 De ...
随机推荐
- 一行代码修复100vh bug | 京东云技术团队
你知道奇怪的移动视口错误(也称为100vh bug)吗?或者如何以正确的方式创建全屏块? 一.100vh bug 什么是移动视口错误? 你是否曾经在网页上创建过全屏元素?只需添加一行 CSS 并不难: ...
- Celery 定义和调用异步任务Task
https://docs.celeryq.dev/en/stable/userguide/tasks.html 使用app.task装饰器定义 需要通过导入celery app,然后使用@app.ta ...
- 华企盾DSC忘记了数据库解锁密码
解决方法:登录数据库控制台,找到DSE所使用数据库默认名字"DSEDB",打开表"FileEncryptKey_TABLE",如下图所示: 第一行,自动生成 ...
- 记一次 .NET某收银软件 非托管泄露分析
一:背景 1. 讲故事 在我的分析之旅中,遇到过很多程序的故障和杀毒软件扯上了关系,有杀毒软件导致的程序卡死,有杀毒软件导致的程序崩溃,这一篇又出现了一个杀毒软件导致的程序非托管内存泄露,真的是分析多 ...
- Log4j2 漏洞复现GetShell
目录: 一.搭建环境 1. 首先拉一个docker镜像 2. 然后启动环境 二.获取shell 首先,试验一下DNSLog 1. 准备JNDI注入工具 下载 进入目录打包成jar包 2. 利用 生成p ...
- JavaFx之模态窗口(二十六)
JavaFx之模态窗口(二十六) 模态窗口:在场景A打开场景B,则A场景无法选择和操作,只能操作B 设置方式,在场景B初始化时设置 stage.initModality(Modality.APPLIC ...
- 【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
问题: 今天Android项目在build时出现了下面的警告: InnerClass annotations are missing corresponding EnclosingMember ann ...
- 教你用Java7的Fork/Join框架开发高并发程序
摘要:Fork/Join框架位于J.U.C(java.util.concurrent)中,是Java7中提供的用于执行并行任务的框架,其可以将大任务分割成若干个小任务,最终汇总每个小任务的结果后得到最 ...
- 9月活动回顾(免费领取PPT)|火山引擎DataLeap、ByteHouse多位专家带来DataOps、实时计算等前沿技术分享!
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 在上月举行的火山引擎开发者社区 Meetup 第12期暨超话数据专场<数智化转型背景下的火山引擎大数据技 ...
- 火山引擎 DataTester:A/B 测试,让企业摆脱广告投放“乱烧钱”
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 在广告投放的场景下,一线广告优化师通常会创建多个计划,去测试不同的广告素材效果.这套方法看似科学,实际上却存在诸多 ...