Java入门教程02:替换文件

系列索引:Java入门教程索引

承接上文,我们来替换jar中的某个class文件。

jar

先看一下jar中里面有什么

1
2
3
4
5
jar tvf helloworld.jar
# 我的输出
0 Tue Dec 31 10:05:00 CST 2024 META-INF/
99 Tue Dec 31 10:05:00 CST 2024 META-INF/MANIFEST.MF
555 Mon Dec 30 17:33:14 CST 2024 helloworld/helloworld.class

如果文件比较多可以使用过滤

1
2
3
jar tvf helloworld.jar | where {$_ -match "class"} # powershell linux下可以使用grep
# 我的输出
555 Mon Dec 30 17:33:14 CST 2024 helloworld/helloworld.class

修改

修改java源码并编译

1
2
3
4
5
6
7
package helloworld;

public class helloworld {
public static void main(String[] args) {
System.out.println("Hello World!!!");
}
}

替换

1
2
3
jar uvf helloworld.jar helloworld/helloworld.class
# 我的输出为
正在添加: helloworld/helloworld.class(输入 = 558) (输出 = 342)(压缩了 38%)

运行

1
2
3
java.exe -jar helloworld.jar
# 我的输出
Hello World!!!

这样就可以在不改动jar中其他文件的情况下修改某个class。

但是有个问题,不是什么时候都有对应源码的。


Java入门教程02:替换文件
https://blog.jackeylea.com/java/how-to-replace-class-file-in-jar/
作者
JackeyLea
发布于
2025年1月2日
许可协议