有时需要修改很多jar(假设这些jar都位于lib目录)中其中一个jar中的某一个类,而且又没有原始代码或ide,这时最简单的方式是:

1 进入lib目录

# cd lib
# ls
test.jar dependency1.jar dependency2.jar

2 查看待修改jar包内类结构

$ unzip -l test.jar

or

$ vim test.jar

3 解压待修改jar包

$ mkdir test_dir
$ unzip test.jar -d test_dir

4 查看class的jdk版本

$ vi -b test_dir/package/Class.class
:%!xxd 0000000: cafe babe 0000 0032 0123 0700 0201 0023 .......2.#.....#

取第15-16位,上例即32,

5 将要修改的文件放到class同目录下(如果没有源代码可以尝试反编译),并修改

$ vi test_dir/package/Class.java

6 收集classpath

由于jar包可能非常多,所以使用命令来生成

$ ls *.jar|sed ':a;N;$!ba;s/\n/:/g'
test.jar:dependency1.jar:dependency2.jar

注linux下是:分隔,windows下是;分隔

7 编译

$ javac -help
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-parameters Generate metadata for reflection on method parameters
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-h <directory> Specify where to place generated native header files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-profile <profile> Check that API used is available in the specified profile
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file

实际命令

$ javac -cp test.jar:dependency1.jar:dependency2.jar -source 1.6 -target 1.6 -sourcepath test_dir -d test_dir test_dir/package/Class.java

8 重新打jar包

$ cd test_dir
$ zip -r test.jar ./*

9 删除临时目录和原始jar

$ cd ..
$ cp test_dir/test.jar test.jar
$ /bin/rm -rf test_dir

10 测试修改生效

【原创】Java基础之简单修改jar包中的class的更多相关文章

  1. IDEA修改jar包中class文件后重新生成jar包

    一.背景 最新想要修改rebeyond大佬的冰蝎项目,特地去网上搜索如何修改jar包中的源码再替换回去的方法,但由于现在的一些文章写的太烂,导致走了很多弯路,因此写下这篇快速使用IDEA修改源码并替换 ...

  2. 修改jar包中class文件

    某日,想要更改jar包中的某个class文件,有无rar无法解压jar文件,故找到如下方式进行操作 1.解压某个jar包:在需要解压的jar包目录下,打开命令行(cmd),输入如下命令,输入:C:\j ...

  3. 27 Java动态加载第三方jar包中的类

    我加载的方法是://参数fileName是jar包的路径,processorName 是业务类的包名+类名public static A load(String fileName, String pr ...

  4. 修改jar包中的文件

    1.用WinRAR压缩软件打开jar包 2.将修改过的文件直接拖拽并覆盖被替换的文件即可

  5. 通过命令直接修改jar包中的静态文件

    1.先将要修改的jar包备份 copy xxx.jar xxx.jar_bak 2.建立一个新的目录便于后面的打包 mkdir jar_tmp 3.将包放到刚刚创建的目录里解压 mv xxx.jar ...

  6. 怎么修改jar包中的class文件然后再重新打成jar包

    1.导入到eclipse中, 修改完了 然后导出jar即可 2.jar cf file.jar *.class 将当前目录下所有CLASS文件打包成新的JAR文件 3.对于补丁.之前已经有jar 例如 ...

  7. Java IO:如何得到Jar包中内嵌Jar包的时间戳

    ClassLoader bladeClassLoader = BladeCLI.class.getClassLoader(); URL url = bladeClassLoader.getResour ...

  8. 曹工杂谈:Linux服务器上,Spring Boot 原地修改 jar 包配置文件/替换class文件,免去重复上传的麻烦

    一.前言 相信很多同学有这样的需求,现在很多公司都有多地的研发中心,经常需要跨地区部署,比如,博主人在成都,但是服务器是北京的.一般城市间网络都不怎么好,上传一个几十兆的jar包那是真的慢,别说现在微 ...

  9. Web项目替换jar包中的文件的方法

    经常遇到这样的问题,需要修改jar包中的方法.应该如何做? 1.有些很人性化的框架jar包,比如SpringSecurity,可以修改配置文件指定一个新建的类,让类实现Jar包中的对应的接口就好了. ...

随机推荐

  1. vue.cli 中使用 less 来写css样式

    vue-cli 的webpack中已配置了less,但 package.json 中没有选项,为了方便开发中使用,需安装一下: 安装方式一: npm install less less-loader ...

  2. sqlserver2012 offset分页

    select ID,UserName from user order by ID OFFSET (10 * (50-1)) ROW FETCH NEXT 10 rows only

  3. 二十、Linux 进程与信号---非局部跳转

    20.1 setjmp 和 longjmp 函数 20.1.1 函数介绍 #include <setjmp.h> int setjmp(jmp_buf env); 函数功能:设置非局部跳转 ...

  4. 【bzoj 2002】弹飞绵羊

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  5. 数据库设计理论与实践·<四>数据库基本术语及其概念

    一.关系模型 关系模型是最重要的一种数据模型.关系数据库模型系统采用关系模型作为数据的组织方式. 关系模型的数据结构: 关系:一张表 元组:一行记录. 属性:一列 [码(键,key)]:表中的某个属性 ...

  6. MLE

    独立同分布的采样x1,x2,…,xn,θ为模型参数,f为我们所使用的模型.参数为θ的模型f产生上述采样可表示为 f(x1,x2,…,xn|θ)=πf(xi|θ) 已知的为x1,x2,…,xn,未知为θ ...

  7. python的基础初始第二天

    1.基础数据类型初始 1,数字类型,int,用于计算,+ ,- ,*, /,加,减,乘,除.在python2有整型和长整型之分(3344L),在python3 已经不区分了. 2,字符串类型strin ...

  8. Jmeter Md5加密操作之-------BeanShell PreProcessor

    背景: 有一些登录会做一些md5校验,通过jmeter的BeanShell可以解决MD5加密情况. 1.首先需要一个解码的jar包,commons-codec-1.10.jar(网上很多),下载后,放 ...

  9. Fragment处理接口回调,网络请求数据

    03-06 19:57:46.138 8691-8691/com.retech.myapplication E/glz: onAttach03-06 19:57:46.138 8691-8691/co ...

  10. META-INF文件夹中的MANIFEST.MF 的作用

    manifest文件是用来描述jar包的,它描述了该jar包的代码是谁的,什么版本,使用什么版本的类库等等....具体如下: 1.基础格式 manifest 文件的格式是很简单的,每一行都是 名-值 ...