久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Java如何修改.class文件變量

瀏覽:3日期:2022-08-25 13:51:22

最近遇到了一個問題,一份很老的代碼要修改里面的變量,源碼早就和開發者一起不知去向,其中引用了一些jar包導致無法直接編譯,只能直接修改.class文件

idea安裝jclasslib-bytecode-viewer插件

標準方式安裝插件

Java如何修改.class文件變量

準備要修改的.class文件

這里我們寫一個簡單的java方法

/** * @Description: * @author: wei.wang * @since: 2020/9/5 11:18 * @history: 1.2020/9/5 created by wei.wang */public class HelloWorld { public static void main(String[] args) { String word = 'Hello World'; System.out.println(word); }}

查找要修改的變量

打開要修改的.class文件,點擊view->Show Bytecode With Jclasslib ,在Constants Pool中使用Text filter功能找到要修改的內容,我們發現有一個String類型常量,指向23,點擊23就能看到要修改的內容

Java如何修改.class文件變量

Java如何修改.class文件變量

修改.class文件中的變量

23是要修改的內容

/** * @Description: * @author: wei.wang * @since: 2020/9/4 19:42 * @history: 1.2020/9/4 created by wei.wang */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 = 'F:GitCodezerotest111targetclassesHelloWorld.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; System.out.println(count); 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());//對23進行修改if(i == 23){ ConstantUtf8Info uInfo = (ConstantUtf8Info)infos[i]; uInfo.setBytes('Hello World HELLO WORLD'.getBytes()); infos[i]=uInfo;} } } cf.setConstantPool(infos); fis.close(); File f = new File(filePath); ClassFileWriter.writeToFile(f, cf); }}

執行結果

可以看到已經修改完成

public class HelloWorld { public HelloWorld() { } public static void main(String[] args) { String word = 'Hello World HELLO WORLD'; System.out.println(word); }}

以上就是Java如何修改.class文件變量的詳細內容,更多關于Java修改文件變量的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 凉山| 洛宁县| 施甸县| 邵阳县| 邹城市| 阿拉尔市| 永清县| 东丰县| 滦平县| 万山特区| 绥德县| 新丰县| 龙泉市| 女性| 丰都县| 双辽市| 江门市| 昌吉市| 内黄县| 清水县| 宁都县| 那坡县| 北海市| 霍城县| 彭水| 高邑县| 邵武市| 通许县| 大理市| 康乐县| 彭水| 阳山县| 株洲县| 松滋市| 宁陵县| 望奎县| 隆林| 金阳县| 象山县| 广西| 蒙自县|