我想编辑一个带有字节码的jar文件,我有2个功能

我想用字节码编辑一个 jar 文件,我创建了一个项目来编辑它。我有两个功能:

    public static byte[] transform(byte[] buf){
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(buf);
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
    for (MethodNode method : classNode.methods) {
        if(method.name.equals(shouldSideBeRenderedMethod) && method.desc.startsWith("(L") && method.desc.endsWith(";IIII)Z")){
            System.out.println("[*] Patching bytecode of shouldSideBeRendered...");
            InsnList insnList = new InsnList();
            Label label0 = new Label();

            insnList.add(new LabelNode(label0));
            insnList.add(new VarInsnNode(ALOAD, 0));
            insnList.add(new FieldInsnNode(GETFIELD, blockClass.replaceAll("\.", "/"), unlocalizedNameField, "Ljava/lang/String;"));
            insnList.add(new LdcInsnNode("stone"));
            insnList.add(new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false));
            Label label1 = new Label();
            insnList.add(new JumpInsnNode(IFNE, new LabelNode(label1)));
            insnList.add(new InsnNode(ICONST_1));
            Label label2 = new Label();
            insnList.add(new JumpInsnNode(GOTO, new LabelNode(label2)));
            insnList.add(new LabelNode(label1));
            insnList.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

            insnList.add(new InsnNode(ICONST_0));
            insnList.add(new LabelNode(label2));
            insnList.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER}));
            insnList.add(new InsnNode(IRETURN));
            Label label3 = new Label();
            insnList.add(new LabelNode(label3));
            method.instructions.insertBefore(method.instructions.getFirst(), insnList);
        }
    }
    return new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES).toByteArray();

}

JarFile jarFile = new JarFile(jarPath);
    List entries = Collections.list(jarFile.entries());
    HashMap classMap = new HashMap();
    entries.forEach(jarEntry -> {
        try {
            if(jarEntry.getName().equals(blockClass.replaceAll("\.", "/") + ".class")){
                classMap.put(jarEntry.getName(), transform(IOUtils.toByteArray(jarFile.getInputStream(jarEntry))));
            }else{
                classMap.put(jarEntry.getName(), IOUtils.toByteArray(jarFile.getInputStream(jarEntry)));

图片[1]-我想编辑一个带有字节码的jar文件,我有2个功能-唐朝资源网

} } catch (IOException e) { e.printStackTrace(); } }); System.out.println("[+] Jar loaded !"); System.out.println("[*] Writing patched jar..."); try { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream("output.jar")); for (Map.Entry entry : classMap.entrySet()) { JarEntry jarEntry = new JarEntry(entry.getKey()); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.write(entry.getValue()); jarOutputStream.closeEntry(); } jarOutputStream.close(); System.out.println("[+] Patched jar successfully written to output.jar !"); } catch (Exception e) { e.printStackTrace(); }

代码有效,我得到了 jar,但是当我启动它时:线程“main”java.lang.UnsupportedClassVersionError 中的异常:net/minecraft/m/d 已由较新版本的 Java 运行时编译(类文件版本 0.0),此版本的 Java Runtime 仅识别最高 5 个的类文件版本2.0

我试图找到一种方法来设置类文件版本但找不到它,知道吗?

© 版权声明
THE END
喜欢就支持一下吧
点赞292 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片