0

I am writing an agent that changes the constructor of java.net.ServerSocket using Javassist. The very simply change I do is to append a call to the default constructor that calls a static method in my class. Even though I see while debugging that the JAR that contains the class (which is the JAR that contains the agent) is available in the classpath, I get a java.lang.NoClassDefFoundError error from java.net.ServerSocket..

Has someone come across such a problem? I am not aware of a limitation in the system classloader that could stop my class my being made available to a class from java.net.

EDIT 1: The relevant code snippet I use for class transformer:

package my.package;

import java.lang.instrument.ClassFileTransformer;
...

public class QClassFileTransformer implements ClassFileTransformer {
...
public byte[] transform(...) {
...
    classPool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer));
    classPool.importPackage("my.package");
    CtClass ctClass = classPool.get(className.replaceAll("/", "."));

    if (!ctClass.isFrozen()) {
        for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
            if (constructor.getParameterTypes().length != 0) {
                continue;
            }
            constructor.insertAfter("my.package.QClassFileTransformer.call(this);");        
            break;
        }

        byte[] bytecode = ctClass.toBytecode();
        ctClass.detach();

        return bytecode;
    }
...

In the transformed class (java.net.ServerSocket), the NoClassDefFoundError exception for my.package.QClassFileTransformer is thrown when the class is loaded.

Ion Ionascu
  • 552
  • 1
  • 4
  • 15

0 Answers0