title: javassist loading a class file in the pre-main method (java instrumentation)
body: <p>I'm trying to load a specific class using javassist, I'm doing this inside a pre-main method as the follwoing:</p>
  
  <pre><code>public byte[] transform(ClassLoader loader, String className, Class&lt;?&gt; classBeingRedefined,
                          ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException 
  {
     byte[] byteCode = classfileBuffer;
  
     if(className.toLowerCase().endsWith("class1"))
     {
         ClassPool classPool = ClassPool.getDefault();
         CtClass ctClass = classPool.get("com.class2");
     }
  }
  </code></pre>
  
  <p>but unfortuanetly, I'm getting a NotFoundException....</p>
  
  <p>com.class2 is a class that will be loaded by the class loader after loading class1, but I nned to add a new method for class1 that has a return type of type com.class2</p>
  
  <p>I tried all solutions inside stackoverflow.com but without any results.....</p>
  
  <p>finally, both class1 and class2 are in the same package and inside the same JAR file, class1 has a member of type class2, but I have no idea why this classpool can't load the second one.</p>
  
  <p>the stack trace here:</p>
  
  <pre><code>Exception: javassist.NotFoundException: com.Class2
   javassist.NotFoundException: com.Class2
  at javassist.ClassPool.get(ClassPool.java:439)
  at javassist.ClassPool.getCtClass(ClassPool.java:504)
  at com.stuff.MainAppletTransformer.transform(MainAppletTransformer.java:69)
  at sun.instrument.TransformerManager.transform(Unknown Source)
  at sun.instrument.InstrumentationImpl.transform(Unknown Source)
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(Unknown Source)
  at java.security.SecureClassLoader.defineClass(Unknown Source)
  at java.net.URLClassLoader.defineClass(Unknown Source)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader.defineClassHelper(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader.access$100(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(Unknown Source)
  at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
  at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
  at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>