Instrumentation Agent没有连接到应用程序

时间:2018-02-09 01:17:54

标签: java instrumentation

我的座席类

package com.mytransformers;

import java.lang.instrument.Instrumentation;

public class MyTransformerAgent {

    public static void premain(String args, Instrumentation inst) {
        MyTransformer transformer = new MyTransformer();
        inst.addTransformer(transformer);
    }

}

我的变形金刚:

public class MyTransformer implements ClassFileTransformer{

    public byte[] transform(ClassLoader arg0, String arg1, Class<?> arg2, ProtectionDomain arg3, byte[] arg4)
            throws IllegalClassFormatException {

        // processing to return some bytes/null;
    }

我使用maven POM构建了我的清单,我可以确认我的jar在META-INF中有以下清单

Manifest-Version: 1.0
Premain-Class: com.mytransformers.MyTransformerAgent
Archiver-Version: Plexus Archiver
Built-By: ha9u63@r
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Class-Path: C:/apache-tomcat-8.5.16/bin/tomcat-juli.jar
Can-Set-Native-Method-Prefix: true
Created-By: Apache Maven 3.0.5
Build-Jdk: 1.8.0_141
Boot-Class-Path: C:/apache-tomcat-8.5.16/lib/tomcat-coyote.jar

我将我的应用程序jar(简单的Hello World 1行语句)和代理jar复制到同一文件夹中并运行以下命令:

java -javaagent:myagent.jar -jar myapp.jar

我不断收到以下错误:

Exception in thread "main" java.lang.ClassNotFoundException: com.mytransformers.MyTransformerAgent
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
        at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
FATAL ERROR in native method: processing of -javaagent failed

这里还缺少什么?我有点困惑。是否有一些我已经破解的类加载委托?

此致

1 个答案:

答案 0 :(得分:0)

嗯...就像Java所说,它无法根据您的清单=在function doReplace(str) { console.log(str); console.log( str.replace(/^```([\s\S]*?)^```/gm, '<quote>$1</quote>') ); } doReplace("```\nThis is a quote\n```"); doReplace("```\nThis is a quote\nand there are some backticks in the text\nbut not ``` at the beginning of a line\n```");中找到类com.mytransformers.MyTransformerAgent

也许可以将您的jar添加到C:/apache-tomcat-8.5.16/lib/tomcat-coyote.jar列表中,例如:
Boot-Class-Path