工具:jclasslib
需要把别人jar包里面的class修改一下信息,
配置文件*.properties MANIFEST.MF 这些东西可以直接用记事本打开修改 然后替换掉就OK..
开始我是先用jd-gui反编译 把我需要修改的那个A.class文件反编译出来把代码保存到A.
Java
,
然后直接在dos里面用javac A.java编译
问题来了..
因为A.java引用了很多其他jar下面的方法 而且A.java有package,
鼓捣了半天以后 最终结果:编译无法正常通过 不能达到直接修改替换掉别人的class文件的目的,再次查资料
看到jclasslib这个东西,可以解决问题。
软件安装: jclasslib
以我自己写的一个小工具 POJOTool.jar 为例
比如我想要修改 不能链接
数据库
的提示信息 为 “芝麻不开门!”
步骤如下:
1.用jd-gui打开该jar 怎么打开不用我说了吧?
打开之后找到 要修改提示信息 的class文件 这里找到的是GenEntity
找到对应需要修改的代码所在的方法名
我这里包含提示信息的这段代码在方法 getAllDataBase() 如下图:
2.用jd-gui或者winrar把GenEntity.class 解压出来到C盘 (目录无所谓)
3.双击桌面上安装好了的 jclasslib bytecode viewer ,点击软件的 File — Open Class File 打开你刚解压出来的class文件
4.点击methods–getAllDataBase–Code
methods是表示方法不用说吧
getAllDataBase是刚在jd-gui里面找到的方法名
Code包含了getAllDataBase方法里所有的信息
找到”不能打开数据库连接,请检查!”
这里这个工具没有提供搜索功能
如果这里很多内容 那么你可以点击 copy to clipboard把这里的内容复制到一个文本文件里面然后再搜索
这里找到的是第82行
5.点击第 82行后面的 #34 会跳转到Constant Pool常量池的第34个常量
6.再点右边的 cp info #362 会跳转到第362个常量
这里能看到String: 不能打开数据库连接,请检查! 也就是最后输出的信息
7.找到GenEntity.class的关键常量了现在就该修改它了
在eclipse里面新建一个Test来处理GenEntity.class
[java]
view plain
copy
import
java.io.*;
import
org.gjt.jclasslib.io.ClassFileWriter;
import
org.gjt.jclasslib.structures.CPInfo;
import
org.gjt.jclasslib.structures.ClassFile;
import
org.gjt.jclasslib.structures.constants.ConstantUtf8Info;
public
class
Test {
public
static
void
main(String[] args)
throws
Exception {
String filePath =
“C:\\GenEntity.class”
;
FileInputStream fis =
new
FileInputStream(filePath);
DataInput di =
new
DataInputStream(fis);
ClassFile cf =
new
ClassFile();
cf.read(di);
CPInfo[] infos = cf.getConstantPool();
int
count = infos.length;
for
(
int
i =
0
; i < count; i++) {
if
(infos[i] !=
null
) {
System.out.print(i);
System.out.print(
” = ”
);
System.out.print(infos[i].getVerbose());
System.out.print(
” = ”
);
System.out.println(infos[i].getTagVerbose());
if
(i ==
362
){
ConstantUtf8Info uInfo = (ConstantUtf8Info)infos[i];
uInfo.setBytes(
“芝麻不开门!”
.getBytes());
infos[i]=uInfo;
}
}
}
cf.setConstantPool(infos);
fis.close();
File f =
new
File(filePath);
ClassFileWriter.writeToFile(f, cf);
}
}
这里需要注意
“C:\\GenEntity.class”
是我存放class的目录
if(i == 362)
这里是刚我在第七步找到的常量序号
“芝麻不开门!”
这里是我想修改的文字信息!
运行Test.java 会有如下提示信息 没有报错就证明正常的 如果报错那么就有问题
9.把C盘刚修改后的GenEntity.class替换掉原来的GenEntity.class
怎么替换不用我说了吧..
用winrar打开.jar 然后把GenEntity.class拖进去覆盖就行了
10.运行效果..
大功告成…