java.lang.NoClassDefFoundError when my Java instrumentation agent class tries to instantiate ClassFileTransformer

277 Views Asked by At

I went through a baledung article on Java instrumentation api and trying to run the code from the article on my machine.

I'm basically trying to do a static load. My agent code is unable to instantiate the ClassFileTransformer class from premain method and fails with NoClassDefFoundError

The code looks like this -

public class MyInstrumentationAgent {
    public static void premain(String agentArgs, Instrumentation inst) {
        // some stuff that is working fine

        AtmTransformer dt = new AtmTransformer(clazz.getName(), classLoader); //fails here after unable to find AtmTransformer class
    }
}

The agent class and transformer class are in the same java package and the agent jar I built contains both the MyInstrumentationAgent.java and AtmTransformer.java.

I ran the code using the following command -

java -javaagent:core-java-jvm-0.0.1-SNAPSHOT-agent.jar -jar core-java-jvm-0.0.1-SNAPSHOT-application.jar

and this is the error stack trace I get -

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:513)
    at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)
Caused by: java.lang.NoClassDefFoundError: javassist/NotFoundException
    at com.baeldung.instrumentation.agent.MyInstrumentationAgent.transform(MyInstrumentationAgent.java:46)
    at com.baeldung.instrumentation.agent.MyInstrumentationAgent.transformClass(MyInstrumentationAgent.java:28)
    at com.baeldung.instrumentation.agent.MyInstrumentationAgent.premain(MyInstrumentationAgent.java:11)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: javassist.NotFoundException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 9 more
*** java.lang.instrument ASSERTION FAILED ***: "result" with message agent load/premain call failed at src/java.instrument/share/native/libinstrument/JPLISAgent.c line: 422

Any thoughts on what I'm missing?

0

There are 0 best solutions below