raphw / raphw/byte-buddy

NoClassDefFound -- new class cannot access the class whose classloader was used for injection

Open
#419 7 comments 0 reactions 1 assignee View on GitHub

@raphw is already working on this.

Since Mar 15, 2018.

documentation question
Dominant language
Java
Stars
6.9k
Forks
855
PR merge metrics
No merged PRs in 30d

Description

In a nutshell, I have a class B$1 that I create from scatch at runtime and that has a non-static method calling a static method of class B$0 (there is no inheritance between the two classes, although they may share the parent, but even that's not a necessity).

Injection is done by

Class b0 = Class.forName("B$0");
ClassLoadingStrategy.Default.INJECTION.load(b0.getClassLoader(),
        Collections.singletonMap(newClass.getTypeDescription(), finishedBytecode));

but if I immediately after that injection test the method

try {
    Class c = Class.forName("B$1");
    Object instance = c.newInstance();
    c.getMethod("move", Object.class).invoke(instance, new Object());
} catch (Exception e) {
    e.printStackTrace();
}

I get a NoClassDefFoundError: B$0.

Why? And how do I resolve this?

(For more details about the use case, please see https://stackoverflow.com/questions/49232664/how-to-load-this-class-in-the-correct-way .)

Oh, and if at all relevant: using Java 1.7.0_65 and ByteBuddy 1.7.9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.