skip to content

A Java agent's transformer injects calls to the agent's own recorder class; instrumented application classes work, but an instrumented JDK class throws NoClassDefFoundError; why?

level: seniorimportance: nice to knowfreq 18%

answer

  1. who defines the agent's classes
  2. bootstrap loader has no parent
  3. resolution uses the caller's loader
  4. Boot-Class-Path bridge
  5. only bridge classes, unique package

basics

~20 s

Classes from a Java agent JAR are defined by the system class loader, but JDK core classes are defined by the bootstrap loader, which cannot see them; the recorder must go on the bootstrap search via Boot-Class-Path or appendToBootstrapClassLoaderSearch.

solid answer

~50 s

The JDK appends the agent JAR to the system class path and loads the agent through the system class loader, into its unnamed module. Injected bytecode resolves its references through the defining loader of the class it sits in. An application class defined by the system loader, or by a child that delegates to it, finds the recorder. A `java.base` class such as `java.net.Socket` is defined by the bootstrap loader, which never consults the system loader, so resolution fails with `NoClassDefFoundError`. The fix is a small bridge, only the recorder API in a unique package, on the bootstrap search through the `Boot-Class-Path` manifest attribute or `Instrumentation.appendToBootstrapClassLoaderSearch`. Bridge classes may link only to bootstrap-defined classes, and the bridge must be added before any JDK class is instrumented. Application loaders whose parent chain skips the system loader hit the same wall.

code

properties · 4 lines
properties
Premain-Class: com.acme.metrics.agent.MetricsAgent
Agent-Class: com.acme.metrics.agent.MetricsAgent
Can-Retransform-Classes: true
Boot-Class-Path: metrics-bridge.jar

go deeper

for a junior

Recall that the JVM has several class loaders and that JDK core classes come from the bootstrap loader, not from the application's class path.

for a middle

Explain that the system class loader defines an agent's classes and that a reference resolves through the loader that defined the class containing it.

for a senior

Diagnose bootstrap-only NoClassDefFoundError after instrumentation and fix it with a minimal bridge on the bootstrap search, appended before any JDK class is touched.

for a principal

Decide whether your agent instruments JDK classes at all, since doing so commits you to maintaining a bootstrap bridge with strict dependency rules.

## Which loader defines an agent's classes When the JVM starts a Java agent, the JDK appends the agent JAR to the **system class path** and loads the agent class through the **system class loader**, the one `ClassLoader.getSystemClassLoader()` returns. Every class the agent JAR contributes, including the recorder its transformer calls into, is defined by that loader and becomes a member of its **unnamed module**. The system loader usually defines the application's `main` class too, so for most application classes the agent and the application share a loader. JDK core classes are different. Classes in `java.base`, such as `java.net.Socket`, are defined by the **bootstrap class loader**, which is built into the JVM and has no parent. It searches the runtime image and its own appended path, and it never asks the system loader. JDK classes defined by the platform loader cannot see the agent either, because the platform loader is the system loader's parent, not its child. ## Why injected calls resolve differently per class A transformer that inserts a call to `Recorder.enter(...)` into a method adds a symbolic reference to `Recorder` to that class's constant pool. When the instrumented method first executes the call, the JVM resolves the reference: 1. It asks the **defining loader of the instrumented class** to find `Recorder`. 2. For an application class, the system loader, or a child loader that delegates up to it, finds `Recorder` on the system class path, and the call works. 3. For a JDK class, the bootstrap loader searches only its own locations, finds nothing, and resolution fails with `NoClassDefFoundError`. 4. The failure sticks: later attempts to resolve the same reference fail with the same error. The rewritten bytecode is equally correct in both places; only what each loader can see differs. ## The bootstrap bridge The standard fix is a small **bridge**: the few classes instrumented JDK code must call, placed on the bootstrap loader's search path. Classes defined by the built-in loaders, and by any loader that follows parent delegation, can then see them. | Mechanism | Declared in | When it applies | Notes | |---|---|---|---| | `Boot-Class-Path` | the agent JAR's manifest | before the agent class is started | space-separated paths; relative ones resolve against the agent JAR's location; for an agent loaded into a running JVM only JAR files count | | `appendToBootstrapClassLoaderSearch(JarFile)` | code in `premain` or `agentmain` | from the moment it is called | may be called several times; JARs are searched in call order | For helpers the application side must see, `appendToSystemClassLoaderSearch(JarFile)` does the same for the system loader. If a custom system class loader is configured, it must implement `appendToClassPathForInstrumentation` for that to work. ## Rules for what goes on the bootstrap search - Put **only** bridge classes there, in a package no application uses. The JDK's own example: if the JAR also held an application's accessor class `C$1`, delegation would let the bootstrap loader define it, and the application would fail with `IllegalAccessError`. - Bridge classes may link **only** to classes the bootstrap loader defines. They cannot call the agent's own classes or its libraries; the usual pattern is a bridge holder whose implementation the agent installs at start-up. - Append the bridge **before** instrumenting any JDK class, since a reference that already failed to resolve keeps failing after the JAR is added. - Keep **one** copy of each bridge class, on the bootstrap search only. A loader that does not delegate first could otherwise define a second copy, and the same name defined by two loaders is two distinct runtime types. ## Other loaders that cannot see the agent The same wall appears wherever a loader's parent chain does not reach the system loader: - loaders created with the platform or bootstrap loader as their parent, as some isolating plugin hosts do; - loaders that look only locally for selected packages and never delegate them. For those, expose the API the instrumented code needs through the bootstrap bridge rather than through the agent JAR. ## Modules are handled for you The module system could have added a second wall, readability. The JVM removes it: it arranges for the module of transformed classes to **read** the unnamed modules of both the bootstrap loader and the loader that defined the agent class. An instrumented `java.base` class can therefore call a bridge class in the bootstrap loader's unnamed module without any `--add-reads` option. Loader visibility is the part the agent must get right.

  • Why must the bridge JAR on the bootstrap search contain only the bridge classes?
    Delegating loaders ask the bootstrap loader before looking locally, so any class in that JAR whose name an application also uses gets defined by the bootstrap loader instead. The JDK's example is an application's non-public accessor class landing there and failing with `IllegalAccessError`. A unique package for the bridge avoids it.
  • What happens if the bridge JAR is appended only after a JDK class already failed to resolve the recorder?
    That reference keeps failing. The Java Virtual Machine Specification makes a failed resolution permanent: later attempts throw the same error, even though the class is now on the search path. The agent must append the bridge before it instruments or retransforms any bootstrap-defined class.

  • Agent classes are loaded by the bootstrap loader, so every class can see them.
  • A java.* class can see any class on the application class path.
  • Putting the whole agent JAR on Boot-Class-Path is a harmless shortcut.
  • The NoClassDefFoundError means the transformer generated invalid bytecode.
  • The JDK module cannot read the agent's module, so --add-reads is the fix.