{"record":{"id":"c6a530b8f5fb9467","repo":"grpc/grpc-java","slug":"provider-rawclass-could-not-be-instantiated-t","errorCode":null,"errorMessage":"Provider ${rawClass} could not be instantiated ${t}","messagePattern":"Provider (.+?) could not be instantiated (.+?)","errorType":"exception","errorClass":"ServiceConfigurationError","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/grpc/ServiceProviders.java","lineNumber":150,"sourceCode":"      }\n      list.add(t);\n    }\n    return list;\n  }\n\n  private static <T> T createForHardCoded(Class<T> klass, Class<?> rawClass) {\n    try {\n      return rawClass.asSubclass(klass).getConstructor().newInstance();\n    } catch (ClassCastException ex) {\n      // Tools like Proguard that perform obfuscation rewrite strings only when the class they\n      // reference is known, as otherwise they wouldn't know its new name. This means some\n      // hard-coded Class.forNames() won't be rewritten. This can cause ClassCastException at\n      // runtime if the class ends up appearing on the classpath but that class is part of a\n      // separate copy of grpc. With tools like Maven Shade Plugin the class wouldn't be found at\n      // all and so would be skipped. We want to skip in this case as well.\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  /**\n   * An interface that allows us to get priority information about a provider.\n   */\n  public interface PriorityAccessor<T> {\n    /**\n     * Checks this provider is available for use, taking the current environment into consideration.\n     * If {@code false}, no other methods are safe to be called.\n     */\n    boolean isAvailable(T provider);\n\n    /**\n     * A priority, from 0 to 10 that this provider should be used, taking the current environment\n     * into consideration. 5 should be considered the default, and then tweaked based on environment\n     * detection. A priority of 0 does not imply that the provider wouldn't work; just that it","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/api/src/main/java/io/grpc/ServiceProviders.java#L132-L168","documentation":"ServiceProviders.createForHardCoded() reflectively instantiates a hard-coded provider class and wraps any Throwable (other than ClassNotFoundException/ClassCastException which are skipped) into a ServiceConfigurationError. This means the provider class was found but its construction failed — bad constructor, initialization exception, missing dependencies, etc.","triggerScenarios":"A hard-coded provider class name is loadable but Provider.newInstance() throws: no-arg constructor missing, static initializer fails, constructor throws due to missing runtime dependency, or the loaded class is from an incompatible gRPC copy causing a cast failure inside the constructor path.","commonSituations":"Shaded/fat jars with multiple gRPC copies on the classpath; a provider (e.g. netty-shaded, okhttp) version mismatched with grpc-core; missing optional dependencies needed by the provider at construction time; security manager blocking instantiation.","solutions":["Fix the classpath so the provider and grpc-core versions match (e.g. align grpc-netty-shaded with grpc-core)","Check the wrapped cause `t` in the message for the actual constructor failure and fix that dependency","Remove duplicate/stale gRPC jars so only one copy exists","Explicitly configure the desired provider via io.grpc.PROVIDER_SELECTOR or -Dio.grpc... provider properties, or disable broken providers via the configurable disabling mechanism"],"exampleFix":"// before (classpath has grpc-netty 1.40 with grpc-core 1.60)\n// ServiceConfigurationError: Provider io.grpc.netty.NettyChannelProvider could not be instantiated java.lang.NoSuchMethodError\n// after: align versions\n// gradle:\nimplementation 'io.grpc:grpc-netty-shaded:1.60.0'\nimplementation 'io.grpc:grpc-core:1.60.0'","handlingStrategy":"try-catch","validationCode":"try (var _ = Class.forName(providerClassName, true, loader)) { /* provider loadable */ }","typeGuard":null,"tryCatchPattern":"try {\n  provider = ServiceProviders.createForHardCoded(cls, args, loader);\n} catch (ServiceConfigurationError e) {\n  logger.warn(\"Provider unavailable, falling back: \" + e.getCause(), e);\n  provider = null; // skip and try next provider\n}","preventionTips":["Keep all io.grpc artifacts at the same version","Avoid shading grpc without relocating all packages consistently","Check the cause chain of ServiceConfigurationError for NoSuchMethodError/NoClassDefFoundError","Run a startup smoke test that loads the expected provider"],"tags":["grpc","provider","service-loading","classpath"],"backgroundTag":"module-init-failed","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}