Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassPool.getDefault(); does nothing in Javassist

Tags:

java

javassist

public byte[] transform(ClassLoader loader, String className, Class<?> clazz,
            ProtectionDomain domain, byte[] bytes)
    throws IllegalClassFormatException {
        return inspectClass(className, clazz, bytes);
}

private byte[] inspectClass(String name, Class<?> clazz, byte[] b) {
        System.out.println("here"); //OK I see this print
        ClassPool pool = ClassPool.getDefault();
        System.out.println("inclass"); //can't see it !!
}

What can happen in ClassPool.getDefault();?

like image 853
JohnJohnGa Avatar asked Oct 07 '11 08:10

JohnJohnGa


1 Answers

I had the same problem, and found ClassPool.getDefault was not throwing Exception, but Throwable. In fact, it was throwing java.lang.NoClassDefFoundError. In my manifest, I had:

Premain-Class: timing.TimingTransform
Boot-Class-Path: lib/javassist.jar

You likely just need to point the Boot-Class-Path to the javassist.jar file. In my case, with the Boot-Class-Path above, I needed a lib directory with javassist.jar in it.

The mistake I made initially was putting javassist.jar inside the agent jar file (THE FOLLOWING IS INCORRECT, FOR DEMONSTRATION PURPOSES ONLY):

     0 Mon Oct 24 16:58:14 MST 2011 META-INF/
   146 Mon Oct 24 16:58:14 MST 2011 META-INF/MANIFEST.MF
     0 Thu Oct 20 14:58:06 MST 2011 timing/
  2482 Mon Oct 24 16:58:06 MST 2011 timing/TimingStats.class
  8360 Mon Oct 24 16:58:06 MST 2011 timing/TimingTransform.class
     0 Tue Oct 18 17:28:24 MST 2011 lib/
645252 Fri Jul 08 18:24:58 MST 2011 lib/javassist.jar

Rather than putting javassist.jar inside the agent jar file, I put it in an external directory accessible to the program. After that change, it worked fine.

like image 72
James Avatar answered Sep 30 '22 08:09

James