java.lang.LinkageError: loader constraint violation: when resolving method "B.m()LFoo;" the class loader (instance of PreemptingClassLoader) of the current class, Task, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, B, have different Class objects for the type /Launcher$AppClassLoader used in the signature

JDK Bug System | Dan Smith | 4 years ago
  1. 0

    When producing a LinkageError due to a ClassLoader constraint violation during method resolution, I get the following error message: Exception in thread "main" java.lang.LinkageError: loader constraint violation: when resolving method "B.m()LFoo;" the class loader (instance of PreemptingClassLoader) of the current class, Task, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, B, have different Class objects for the type sun/misc/Launcher$AppClassLoader used in the signature at Task.run(Task.java:8) at Test.main(Test.java:7) Two problems: 1) sun/misc/Launcher$AppClassLoader is _not_ the ClassLoader of B. It is the ClassLoader of the resolved method's class, A (which is a supertype of B). 2) "the type sun/misc/Launcher$AppClassLoader used in the signature" should read "the type Foo used in the signature" Sources to reproduce follow. ---------- public class Foo {} ---------- public class A { public Foo m() { return null; } } ---------- public class B extends A {} ---------- public class Task implements Runnable { public void run() { Class<?> c = Foo.class; B b = new B(); b.m(); }} ---------- public class Test { public static void main(String... args) throws Exception { Class<?> c = Foo.class; ClassLoader l = new PreemptingClassLoader("B", "Foo", "Task"); Runnable r = (Runnable) l.loadClass("Task").newInstance(); r.run(); }} ---------- import java.util.*; import java.io.*; public class PreemptingClassLoader extends ClassLoader { private final Set<String> names = new HashSet<>(); public PreemptingClassLoader(String... names) { for (String n : names) this.names.add(n); } protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { if (!names.contains(name)) return super.loadClass(name, resolve); Class<?> result = findLoadedClass(name); if (result == null) { String filename = name.replace('.', '/') + ".class"; try (InputStream data = getResourceAsStream(filename)) { if (data == null) throw new ClassNotFoundException(); try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { int b; do { b = data.read(); if (b >= 0) buffer.write(b); } while (b >= 0); byte[] bytes = buffer.toByteArray(); result = defineClass(name, bytes, 0, bytes.length); } } catch (IOException e) { throw new ClassNotFoundException("Error reading class file", e); } } if (resolve) resolveClass(result); return result; } } ----------

    JDK Bug System | 4 years ago | Dan Smith
    java.lang.LinkageError: loader constraint violation: when resolving method "B.m()LFoo;" the class loader (instance of PreemptingClassLoader) of the current class, Task, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, B, have different Class objects for the type /Launcher$AppClassLoader used in the signature
  2. 0

    LinkageError while starting tomcat from eclipse - Tech Forum Network

    techforumnetwork.com | 11 months ago
    java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type LoggerFactory; used in the signature
  3. 0

    LinkageError while starting tomcat from eclipse

    Stack Overflow | 2 years ago | KarlsFriend
    java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type LoggerFactory; used in the signature
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Cannot get logging to work right with embedded Jetty

    soso.io | 1 year ago
    java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/eclipse/jetty/webapp/WebAppClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type LoggerFactory; used in the signature
  6. 0

    weblogic 10.3.4 java.lang.ClassNotFoundException: javax.el.ELContextListener

    Stack Overflow | 5 years ago | Mustafa
    java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.addELResolver(Ljavax/el/ELResolver;)V" the class loader (instance of weblogic/utils/classloaders/GenericClassLoader) of the current class, com/sun/faces/config/ConfigureListener, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type javax/el/ELResolver used in the signature

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.LinkageError

      loader constraint violation: when resolving method "B.m()LFoo;" the class loader (instance of PreemptingClassLoader) of the current class, Task, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, B, have different Class objects for the type /Launcher$AppClassLoader used in the signature

      at Task.run()
    2. Unknown
      Test.main
      1. Task.run(Task.java:5)
      2. Test.main(Test.java:8)
      2 frames