{"record":{"id":"33a5586dc2e5395c","repo":"quarkusio/quarkus","slug":"provider-s-could-not-be-instantiated-s","errorCode":null,"errorMessage":"Provider %s could not be instantiated %s","messagePattern":"Provider (.+?) could not be instantiated (.+?)","errorType":"exception","errorClass":"ServiceConfigurationError","httpStatus":null,"severity":"error","filePath":"extensions/grpc-common/runtime/src/main/java/io/quarkus/grpc/common/runtime/graal/GrpcSubstitutions.java","lineNumber":36,"sourceCode":"import sun.misc.Unsafe;\n\n@SuppressWarnings(\"unused\")\n@TargetClass(className = \"io.grpc.ServiceProviders\")\nfinal class Target_io_grpc_ServiceProviders { // NOSONAR\n\n    @Substitute\n    static boolean isAndroid(ClassLoader cl) {\n        return false;\n    }\n\n    @Substitute\n    private static <T> T createForHardCoded(Class<T> klass, Class<?> rawClass) {\n        try {\n            return rawClass.asSubclass(klass).getConstructor().newInstance();\n        } catch (NoSuchMethodException | ClassCastException e) {\n            return null;\n        } catch (Throwable t) {\n            throw new ServiceConfigurationError(\n                    String.format(\"Provider %s could not be instantiated %s\", rawClass.getName(), t), t);\n        }\n    }\n\n    @Substitute\n    public static <T> List<T> loadAll(\n            Class<T> klass,\n            Iterator<T> serviceLoader,\n            Supplier<Iterable<Class<?>>> hardcoded,\n            final Target_io_grpc_ServiceProviders_PriorityAccessor<T> priorityAccessor) {\n\n        // For loading classes directly instead of using SPI.\n\n        Iterable<T> candidates;\n        candidates = getCandidatesViaHardCoded(klass, hardcoded.get());\n        List<T> list = new ArrayList<>();\n        for (T current : candidates) {\n            if (!priorityAccessor.isAvailable(current)) {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/grpc-common/runtime/src/main/java/io/quarkus/grpc/common/runtime/graal/GrpcSubstitutions.java#L18-L54","documentation":"This substitution emulates java.util.ServiceLoader for hard-coded provider classes in native image. createForHardCoded instantiates a provider via its no-arg constructor; if instantiation fails for any reason other than missing constructor or wrong type, it throws ServiceConfigurationError 'Provider <name> could not be instantiated <cause>'.","triggerScenarios":"A hard-coded gRPC provider class (e.g. a NameResolverProvider or LoadBalancerProvider registered for native image) whose no-arg constructor exists but throws during construction in the native image.","commonSituations":"Provider static init performing operations unsupported in native image; provider constructor depending on missing resources/classes not registered for reflection; GraalVM build-time init clashing with runtime init.","solutions":["Inspect the chained cause and fix the provider initialization failing in native mode","Register the provider's classes/resources for reflection with @RegisterForReflection or a RuntimeHints registrar","Adjust the provider's initialization to be native-compatible (avoid unsupported APIs, use build-time init)","Pin/update gRPC and provider versions so the provider is known to work on native image"],"exampleFix":"// before\nclass MyProvider extends NameResolverProvider {\n    MyProvider() { loadExternalConfig(); } // fails in native\n}\n// after\nclass MyProvider extends NameResolverProvider {\n    MyProvider() { /* only native-safe init */ }\n}","handlingStrategy":"try-catch","validationCode":"try {\n    Object p = klass.getDeclaredConstructor().newInstance();\n} catch (Throwable t) {\n    LOG.errorf(\"Provider %s will fail in native image: %s\", klass.getName(), t);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ServiceLoader.load(NameResolverProvider.class).forEach(p -> registry.add(p));\n} catch (ServiceConfigurationError e) {\n    LOG.errorf(e, \"Provider could not be instantiated: %s\", e.getMessage());\n}","preventionTips":["Keep provider constructors trivial and native-safe","Register provider classes with reflection hints","Test ServiceLoader-based loading under native image early"],"tags":["grpc","native-image","serviceloader","graalvm"],"backgroundTag":"service-provider-init-failed","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}