Sunday, November 19, 2017

Exception in thread "main" javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of sun/misc/Launcher$AppClassLoader): attempted duplicate class definition for name:

Code:

package testjavasisit;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

public class TestJavasisit {
    /**
     * @param args
     *            the command line arguments
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        ClassPool pool = ClassPool.getDefault();

        // version original
         Hello h1 = new Hello(); // remove this line
         h1.say();               // remove this line

        CtClass cc = pool.get("testjavasisit.Hello");
        cc.defrost();
        CtMethod m = cc.getDeclaredMethod("say");
        m.insertBefore("{ System.out.println(\"Hello.say():\"); }");
        cc.writeFile(".");
        // This throws a java.lang.LinkageError ... attempted  duplicate class definition for name: ...
        cc.toClass();
        // version modifie
        Hello h2 = new Hello();
        h2.say();
    }

}

Debug and solution:

If you remove the following 2 lines, then it will run successfully.
   // Hello h1 = new Hello();
   // h1.say();
Output:
Hello.say():
Hello

Root Cause Analysis:

When first time you use Hello h1 = new Hello();, the classloader loads the Hello class.
After that when you again try to load the Hello class by using cc.toClass();, this error comes.

Reason of occurrence:

Rafael Winterhalter told the reason and some way of solutions in this link as
cc.toClass() takes a loaded class[Hello] and redefines this very same class without changing its name. After this redefinition, you attempt to load the altered class one more time. This is however not possible in Java where any ClassLoader can only load a class of a given name one single time.
To overcome your problem, you have different choices:
  1. Create a subclass of the argument class (or use interfaces) which uses a random name. The subclass is then type compatible to your argument class but is never loaded.
  2. Use the Instrumentation API to redefine your loaded class at runtime.
  3. Make sure that the input class and the output class are loaded with different ClassLoaders. (not recommended)

Same type of issues are described here:

In tomcat, they have solved the issue.
  1. https://github.com/brettwooldridge/HikariCP/issues/217
  2. http://forum.spring.io/forum/spring-projects/container/58952-use-javassist-modify-class-error-when-in-lib
Resource Link: https://stackoverflow.com/a/40236945/2293534

No comments:

Post a Comment