当前位置: 首页 > 知识库问答 >
问题:

类由最新版本的Java Runtime(0.0)编译,此版本的Java Runtime仅识别高达52.0的类文件版本

嵇昱
2023-03-14

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

    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<JarEntry> entries = Collections.list(jarFile.entries());
    HashMap<String, byte[]> 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)));
            }
        } 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<String, byte[]> 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.Unsupport tedClassVersionError: net/minecraft/m/d已由Java运行时(类文件版本0.0)的最新版本编译,该版本的Java运行时仅识别高达52.0的类文件版本

我试图找到一种方法来设置类文件版本,但找不到一个,有什么想法吗?

共有1个答案

栾峰
2023-03-14

你的陈述

return new ClassWriter(classReader,
        ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES).toByteArray();

创建一个新的类编写器,但在请求生成的字节码之前不向其写入任何内容。

您必须用类信息填充它。在您的具体情况下:

ClassWriter cw = new ClassWriter(classReader,
                                 ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(cw);
return cw.toByteArray();

比如classReader。accept(classNode,ClassReader.EXPAND_FRAMES)将所有信息从类读取器复制到类节点。接受(cw)将修改后的信息从类节点复制到类编写器。

从字面上回答这个问题,调用access(int version, int access,java.lang.String name,java.lang.String签名,java.lang.String overName,java.lang.String[]接口)将指定类的主要信息,包括版本号,但是,使用Visitor Pattern时将自动调用该方法。

 类似资料: