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属性的一般步骤:

  1. 打开项目的pom.xml文件。

  2. 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 or false, case irrelevant). Is the ability to redefine classes needed by this agent. Values other than true are considered false. This attribute is optional, the default is false.

    • Can-Retransform-Classes

      Boolean (true or false, case irrelevant). Is the ability to retransform classes needed by this agent. Values other than true are considered false. This attribute is optional, the default is false.

    • Can-Set-Native-Method-Prefix

      Boolean (true or false, case irrelevant). Is the ability to set native method prefix needed by this agent. Values other than true are considered false. This attribute is optional, the default is false.

    An agent JAR file may have both the Premain-Class and Agent-Class attributes present in the manifest. When the agent is started on the command-line using the -javaagent option then the Premain-Class attribute specifies the name of the agent class and the Agent-Class attribute is ignored. Similarly, if the agent is started sometime after the VM has started, then the Agent-Class attribute specifies the name of the agent class (the value of Premain-Class attribute is ignored).

  3. 保存pom.xml文件。

  4. 使用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中?的更多相关文章

  1. build path libraries java基础--Jar包添加到build path方式说明--01

    摘自: http://blog.csdn.net/haolongabc/article/details/7007701 java基础--Jar包添加到build path方式说明--01 前言:这段短 ...

  2. 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)

    原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...

  3. 将一个jar包放到linux下定时执行

    将一个jar包放到linux下定时执行 1.在dbtodb文件夹下新建一个dbtodb.sh,脚本内容为: #!/bin/bash cd /usr/dbtodb/ java -jar dbtodb.j ...

  4. IDEA中将WEB-INF\lib下的Jar包添加到项目中

    打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S]左侧选中Modules,在Dependecies中,点击右侧“+”号,选择JARS or directorie ...

  5. 从jar包还原出java源码(项目文件)

    原文转载至:https://blog.csdn.net/mxmxz/article/details/73043156 上周接到个新任务,一个遗留的接口工程需要改造,然而根据前任开发留下的文档看,这个工 ...

  6. maven 将jar包添加到本地仓库

      maven  如何将jar包添加到本地仓库 CreateTime--2018年4月19日12:50:50 Author:Marydon 情景描述:当项目所需的jar包,maven中央仓库中没有该j ...

  7. maven 如何将自己的jar包添加到本地仓库

    1 准备一个需要添加到本地仓库的jar包 我这里准备了一个名为mail.jar 的jar包,放到E:\Install Files目录下面 2 下面演示如何将准备的jar包添加到本地仓库 1 语法 mv ...

  8. IDEA:将WEB-INF\lib下的Jar包添加到项目中

    打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S] 左侧选中Modules,在Dependecies中,点击右侧"+"号,选择JARS or ...

  9. 本地jar包添加至Maven仓库

    Maven命令将本地的jar包方放到maven仓库中 //自定义本地的jar包在pom文件的参数 <dependency> <groupId>com.eee</group ...

  10. springboot使用jar包方式启动,找不到resources目录中的配置文件(运行时)FileNotFoundException

    将springboot项目打包成jar包,使用 java -jar jar包进行启动,富文本框使用ckeditor+ckfinder: 因为ckfinder自定义配置文件了,上传图片时出现了异常 De ...

随机推荐

  1. 部署堡垒机5——安装Core

    部署jumpserver服务核心组件Core 一.前期准备 一个后台程序,基本上都是需要依赖于数据库才能运行,后台程序在启动的时候,代码就回去连接数据库,保证数据库,正确启动,且可以正确连接,否则后台 ...

  2. Spring MVC 源码解析

    本文的 MVC 基于传统的 Servlet 应用,所使用的 Spring Web 的环境为 Spring Boot 2.5.2 静态资源的加载 参考 Spring Boot 中给出的文档,原文如下: ...

  3. wpf 叫号系统

    wpf 叫号系统 桌面版 wpf 叫号系统 C# .Net 4.8 WPF 数据库 SQLServer 2012 数据队列 Redis 日志 log4net 叫号系统客户端登陆 设置,职称设置,科室和 ...

  4. Javac多模块化编译

    转载:原文链接 从SDK9开始,Java支持多模块编译.那么,怎么用javac实现多模块编译呢? 项目介绍 先来看看我们的项目. 首先lib文件夹下是依赖模块,有一个hello模块.hello模块包含 ...

  5. Flutter定时器

    代码 const timeout = Duration(seconds: 3); var t=Timer.periodic(timeout, (timer) { print('afterTimer=' ...

  6. 云图说丨初识华为云OrgID

    本文分享自华为云社区<[云图说]第282期 初识华为云OrgID:轻松实现统一帐号.统一授权>,作者: 码上开花_Lancer. 组织成员帐号 OrgID是面向企业提供组织管理.企业成员帐 ...

  7. 云图说 | 图解制品仓库服务CodeArts Artifact

    本文分享自华为云社区<[云图说]第277期 图解制品仓库CodeArts Artifact>,作者:阅识风云. 制品仓库服务CodeArts Artifact用于存放源码编译生成的.可运行 ...

  8. 消除视觉Transformer与卷积神经网络在小数据集上的差距

    摘要:本文通过多种操作构建混合模型,增强视觉Transformer捕捉空间相关性的能力和其进行通道多样性表征的能力,弥补了Transformer在小数据集上从头训练的精度与传统的卷积神经网络之间的差距 ...

  9. IOS上架流程详解,包含审核避坑指南!

    ​ 准备 开发者账号 完工的项目 上架步骤 一.创建App ID 二.创建证书请求文件 (CSR文件) 三.创建发布证书 (CER) 四.创建Provisioning Profiles配置文件 (PP ...

  10. ES6常用知识学习札记

    转载请注明出处 原文连接 http://blog.huanghanlian.com/article/5c7aa6c7bf3acc0864870f9d es6 是什么 首先弄明白ECMA和js的关系.E ...