{"record":{"id":"872805f6271002a4","repo":"apache/maven","slug":"error-while-discovering-di-classes-from-classloade","errorCode":null,"errorMessage":"Error while discovering DI classes from classLoader","messagePattern":"Error while discovering DI classes from classLoader","errorType":"exception","errorClass":"DIException","httpStatus":null,"severity":"error","filePath":"impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java","lineNumber":114,"sourceCode":"            while (enumeration.hasMoreElements()) {\n                URL url = enumeration.nextElement();\n                if (loadedUrls.add(url.toExternalForm())) {\n                    try (InputStream is = url.openStream();\n                            BufferedReader reader =\n                                    new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {\n                        for (String line : reader.lines()\n                                .map(String::trim)\n                                .filter(l -> !l.isEmpty())\n                                .filter(l -> !l.startsWith(\"#\"))\n                                .toList()) {\n                            Class<?> clazz = classLoader.loadClass(line);\n                            bindImplicit(clazz);\n                        }\n                    }\n                }\n            }\n        } catch (Exception e) {\n            throw new DIException(\"Error while discovering DI classes from classLoader\", e);\n        }\n        return this;\n    }\n\n    @Nonnull\n    @Override\n    public Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Scope scope) {\n        return bindScope(scopeAnnotation, () -> scope);\n    }\n\n    @Nonnull\n    @Override\n    public Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Supplier<Scope> scope) {\n        if (scopes.put(scopeAnnotation, scope) != null) {\n            throw new DIException(\n                    \"Cannot rebind scope annotation class to a different implementation: \" + scopeAnnotation);\n        }\n        return this;","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java#L96-L132","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"<!-- before: maven-shade-plugin overwrites the descriptor -->\n<!-- after: append entries from merged jars -->\n<transformer implementation=\"org.apache.maven.plugins.shade.resource.AppendingTransformer\">\n  <resource>META-INF/maven/org.apache.maven.api.di.Inject</resource>\n</transformer>","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    injector.discover(classLoader);\n} catch (DIException e) {\n    Throwable cause = e.getCause();\n    if (cause instanceof ClassNotFoundException cnfe) {\n        // stale META-INF/maven/org.apache.maven.api.di.Inject entry: clean and rebuild\n        log.error(\"stale DI descriptor lists {}\", cnfe.getMessage(), e);\n    }\n    throw e;\n}","preventionTips":["Clean-build after renaming or removing DI-annotated classes","When shading, explicitly merge META-INF/maven/org.apache.maven.api.di.Inject files"],"tags":["di","classloader","service-file","annotation-processor","maven"],"backgroundTag":"service-loader-class-not-found","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}