apache/maven · error · DIException
Error while discovering DI classes from classLoader
Error message
Error while discovering DI classes from classLoader
What it means
Injector.discover(ClassLoader) scans every META-INF/maven/org.apache.maven.api.di.Inject resource — files the DI annotation processor generates to list classes for implicit binding — loads each listed class, and binds it. Any failure while enumerating resources, reading a file, or loading/binding a listed class is wrapped in DIException 'Error while discovering DI classes from classLoader'.
Source
Thrown at impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java:114
while (enumeration.hasMoreElements()) {
URL url = enumeration.nextElement();
if (loadedUrls.add(url.toExternalForm())) {
try (InputStream is = url.openStream();
BufferedReader reader =
new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
for (String line : reader.lines()
.map(String::trim)
.filter(l -> !l.isEmpty())
.filter(l -> !l.startsWith("#"))
.toList()) {
Class<?> clazz = classLoader.loadClass(line);
bindImplicit(clazz);
}
}
}
}
} catch (Exception e) {
throw new DIException("Error while discovering DI classes from classLoader", e);
}
return this;
}
@Nonnull
@Override
public Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Scope scope) {
return bindScope(scopeAnnotation, () -> scope);
}
@Nonnull
@Override
public Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Supplier<Scope> scope) {
if (scopes.put(scopeAnnotation, scope) != null) {
throw new DIException(
"Cannot rebind scope annotation class to a different implementation: " + scopeAnnotation);
}
return this;View on GitHub (pinned to e4093d4e12)
Solutions
- Clean-build so the annotation processor regenerates META-INF/maven/org.apache.maven.api.di.Inject (mvn clean install)
- Configure shading to append/merge these descriptor files instead of overwriting them
- Read the DIException cause: ClassNotFoundException names the exact missing class — fix or remove that entry's source module
- Ensure the classloader passed to discover() is the one that actually contains the listed classes
Example fix
<!-- before: maven-shade-plugin overwrites the descriptor --> <!-- after: append entries from merged jars --> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/maven/org.apache.maven.api.di.Inject</resource> </transformer>
Defensive patterns
Strategy: try-catch
Try / catch
try {
injector.discover(classLoader);
} catch (DIException e) {
Throwable cause = e.getCause();
if (cause instanceof ClassNotFoundException cnfe) {
// stale META-INF/maven/org.apache.maven.api.di.Inject entry: clean and rebuild
log.error("stale DI descriptor lists {}", cnfe.getMessage(), e);
}
throw e;
} Prevention
- Clean-build after renaming or removing DI-annotated classes
- When shading, explicitly merge META-INF/maven/org.apache.maven.api.di.Inject files
When it happens
Trigger: discover() hitting a resource that lists a class name which no longer exists (ClassNotFoundException), an unreadable or corrupt resource (IOException), or a listed class whose binding fails.
Common situations: Stale annotation-processor output after incremental compilation or refactors; shaded or merged jars where the descriptor file was overwritten instead of concatenated; classes listed in a different classloader layer (JPMS or isolating loaders).
Related errors
- A required class was missing while executing {}: {}
- A type incompatibility occurred while executing ${mojoDescri
- Could not determine current Maven version
- Expected type from key {} to be parameterized
- Error while initializing binding {}
AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21).
Data as JSON: /api/errors/872805f6271002a4.
Report an issue: GitHub.