为什么我得到 AgentLoadException?

Why am i getting AgentLoadException?

这是我第一次使用代理,我去了任务管理器并选择了我的 minecraft 的 pid,当我这样做时 > jvm.attach(myAgent);我得到 AgentLoadException。 装载机:

File agentFile = new File("src/main/resources/jar/agent.jar");
VirtualMachine jvm = VirtualMachine.attach(pid);
jvm.loadAgent(agentFile.getAbsolutePath());
jvm.detach();

代理人:

 public static void premain(String agentArgs, Instrumentation inst) {
        String className = "net.minecraft.client.Minecraft";
        transformClass(className, inst);
    }

    public static void agentmain(String agentArgs, Instrumentation inst) {
        String className = "net.minecraft.client.Minecraft";
        transformClass(className, inst);
    }

    private static void transformClass(String className, Instrumentation instrumentation) {
        Class<?> targetClass = null;
        ClassLoader targetClassLoader = null;
        try {
            targetClass = Class.forName(className);
            targetClassLoader = targetClass.getClassLoader();
            tranform(targetClass, targetClassLoader, instrumentation);
            return;
        } catch (Exception exception) {
            System.out.println("An error ocurred in transformClass()");
        }
        for (Class<?> clazz : instrumentation.getAllLoadedClasses()) {
            if (clazz.getName().equals(className)) {
                targetClass = clazz;
                targetClassLoader = targetClass.getClassLoader();
                tranform(targetClass, targetClassLoader, instrumentation);
                return;
            }
        }
        throw new RuntimeException("Failed to find class[" + className + "]");
    }

    private static void tranform(Class<?> clazz, ClassLoader classLoader, Instrumentation instrumentation) {
        MinecraftTransformer dt = new MinecraftTransformer(clazz.getName(), classLoader);
        instrumentation.addTransformer(dt, true);
        try {
            instrumentation.retransformClasses(clazz);
        } catch (Exception ex) {
            throw new RuntimeException("Transform failed for: [" + clazz.getName() + "]");
        }
    }

    public static class MinecraftTransformer implements ClassFileTransformer {

        private final String METHOD = "runGameLoop";
        private  String targetClassName;
        private  ClassLoader targetClassLoader;

        public MinecraftTransformer(String targetClassName, ClassLoader classLoader) {
            this.targetClassLoader = classLoader;
            this.targetClassName = "net.minecraft.client.Minecraft";
        }

        @Override
        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            byte[] byteCode = classfileBuffer;
            String finalTargetClassName = targetClassName.replaceAll("\.", "/");
            if (!className.equals(finalTargetClassName)) {
                return byteCode;
            }
            if (className.equals(finalTargetClassName) && loader.equals(targetClassLoader)) {
                System.out.println("Transforming the class");
                try {
                   
                    ClassPool cp = ClassPool.getDefault();
                    CtClass cc = cp.get(this.targetClassName);
                    CtMethod m = cc.getDeclaredMethod(METHOD);
                    StringBuilder startBlock = new StringBuilder();
                    startBlock.append("System.out.println(\"Agent works\");");
                    m.insertBefore(startBlock.toString());
                    byteCode = cc.toBytecode();
                    cc.detach();
                } catch (NotFoundException | CannotCompileException | IOException exception) {
                    System.out.println("error en agent");
                }
            }
            return byteCode;
        }
    }

我一直在阅读有关此异常的文档,并提供有关额外信息的 0 条信息(我得到 100 条),这可能是因为某些安全问题还是因为我的代理人做错了什么?

堆栈跟踪:

com.sun.tools.attach.AgentLoadException: 100
    at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachine.java:109)
    at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgentLibrary(HotSpotVirtualMachine.java:120)
    at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:148)
    at jdk.attach/com.sun.tools.attach.VirtualMachine.loadAgent(VirtualMachine.java:538)
    at Launcher.main(Launcher.java:16)

pom.xml 代理人:

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/custom/MANIFEST.MF</manifestFile>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

清单文件:

Agent-Class: Agent
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Premain-Class: Agent

查看代理的另一个示例 (https://github.com/fstab/promagent/blob/5f05bbcac3ea08e2a02c4a50414511e79325b07d/promagent-framework/promagent-loader/src/main/java/io/promagent/loader/PromagentLoader.java#L48),错误代码 100 似乎表示未正确找到代理 JAR,或者 Agent-Class 属性在清单。

您目前正在尝试 create/set 带有 maven-site-plugin 的清单文件。此插件用于为您的项目生成网站。 参见 https://maven.apache.org/plugins/maven-site-plugin/

我想你要么想要 maven-archiver https://maven.apache.org/shared/maven-archiver/ or the maven-jar-plugin to customize your manifest: https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html

清单的默认位置也是 src/main/resources/META-INF/MANIFEST.MF,您可以尝试将其从当前位置移开 src/main/resources/custom/MANIFEST.MF

您还可以尝试以下任何有关如何创建自己的代理的教程:

我建议您使用反编译器打开项目生成的 jar 文件并检查该 jar 中的清单文件的内容,例如使用 https://github.com/java-decompiler/jd-gui