I’m trying to run a gRPC Kotlin client using Spring Boot, but I’m encountering the following error when executing my main function:
java.lang.NoClassDefFoundError: io/grpc/ClientStreamTracer$InternalLimitedInfoFactory
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1023)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
at io.grpc.internal.ManagedChannelImpl$ChannelStreamProvider.newStream(ManagedChannelImpl.java:591)
at io.grpc.internal.ClientCallImpl.startInternal(ClientCallImpl.java:256)
at io.grpc.internal.ClientCallImpl.start(ClientCallImpl.java:189)
at io.grpc.internal.DelayedClientCall$1.run(DelayedClientCall.java:180)
at io.grpc.internal.DelayedClientCall.drainPendingCalls(DelayedClientCall.java:278)
at io.grpc.internal.DelayedClientCall.setCall(DelayedClientCall.java:152)
at io.grpc.internal.ManagedChannelImpl$RealChannel$PendingCall$1.run(ManagedChannelImpl.java:1107)
at io.grpc.stub.ClientCalls$ThreadlessExecutor.runQuietly(ClientCalls.java:777)
at io.grpc.stub.ClientCalls$ThreadlessExecutor.waitAndDrain(ClientCalls.java:760)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:161)
at com.example.grpc.HelloWorldServiceGrpc$HelloWorldServiceBlockingStub.sayHello(HelloWorldServiceGrpc.java:157)
at com.example.howbeapiserver.HelloClient.sayHelloWithBlocking(HelloClient.kt:26)
at com.example.howbeapiserver.HowbeApiserverApplicationKt.main(HowbeApiserverApplication.kt:11)
Caused by: java.lang.ClassNotFoundException: io.grpc.ClientStreamTracer$InternalLimitedInfoFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
... 22 more
Here are the relevant parts of my project setup:
build.gradle.kts:
import com.google.protobuf.gradle.*
plugins {
id("org.springframework.boot") version "3.3.2"
id("io.spring.dependency-management") version "1.1.6"
kotlin("jvm") version "1.9.24"
kotlin("plugin.spring") version "1.9.24"
id("com.google.protobuf") version "0.9.4"
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
val grpcKotlinVersion by extra("1.4.1")
val grpcVersion by extra("1.65.1")
val grpcProtobufVersion by extra("4.27.2")
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("io.grpc:grpc-kotlin-stub:$grpcKotlinVersion")
implementation("io.grpc:grpc-stub:$grpcVersion")
implementation("io.grpc:grpc-protobuf:$grpcVersion")
implementation("io.grpc:grpc-netty-shaded:$grpcVersion")
implementation("com.google.protobuf:protobuf-kotlin:$grpcProtobufVersion")
}
sourceSets {
getByName("main") {
java {
srcDirs(
"build/generated/source/proto/main/java",
"build/generated/source/proto/main/kotlin"
)
}
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$grpcProtobufVersion"
}
plugins {
create("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
}
create("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:$grpcKotlinVersion:jdk8@jar"
}
}
generateProtoTasks {
all().forEach {
it.plugins {
create("grpc")
create("grpckt")
}
it.builtins {
create("kotlin")
}
}
}
}
HelloClient.kt:
package com.example.howbeapiserver
import com.example.grpc.HelloRequest
import com.example.grpc.HelloReply
import com.example.grpc.HelloWorldServiceGrpc
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
class HelloClient(host: String?, port: Int) {
private var managedChannel: ManagedChannel? = null
private var blockingStub: HelloWorldServiceGrpc.HelloWorldServiceBlockingStub? = null
private var asyncStub: HelloWorldServiceGrpc.HelloWorldServiceStub? = null
init {
managedChannel = ManagedChannelBuilder
.forAddress(host, port)
.usePlaintext()
.build()
blockingStub = HelloWorldServiceGrpc.newBlockingStub(managedChannel)
asyncStub = HelloWorldServiceGrpc.newStub(managedChannel)
}
fun sayHelloWithBlocking(message: String): HelloReply {
val request = buildRequest(message)
return blockingStub!!.sayHello(request)
}
private fun buildRequest(message: String): HelloRequest {
return HelloRequest.newBuilder()
.setName(message)
.build()
}
}
HowbeApiserverApplication.kt:
package com.example.howbeapiserver
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class HowbeApiserverApplication
fun main(args: Array<String>) {
//runApplication<HowbeApiserverApplication>(*args)
val helloClient = HelloClient("localhost", 50051)
println(helloClient.sayHelloWithBlocking("hello world").message)
}
It appears that there is a class loading issue with io.grpc.ClientStreamTracer$InternalLimitedInfoFactory
. I have ensured that all dependencies are correctly defined in my build.gradle.kts
.
Any ideas on what might be causing this issue and how to resolve it?
Sites referenced : https://goslim56.tistory.com/26
I tried adding the necessary dependencies exactly as specified in multiple reference sites. Despite following these guidelines precisely, I am still encountering the java.lang.NoClassDefFoundError: io/grpc/ClientStreamTracer$InternalLimitedInfoFactory error. I expected the application to run without issues, but it fails with the mentioned class loading error.
정하우 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.