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 the method's defining class, A, have different Class objects for the type Foo used in the signature

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • 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; } } ----------
    via by 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 the method's defining class, A, have different Class objects for the type Foo used in the signature at Task.run(Task.java:5) at Test.main(Test.java:8)
    No Bugmate found.