open-telemetry / opentelemetry-java-examples

https://opentelemetry.io
Apache License 2.0
201 stars 120 forks source link

java.lang.NoClassDefFoundError: io/opentelemetry/context/ImplicitContextKeyed exception when following the OTLP Java example. #360

Open Micah-Gafford opened 6 months ago

Micah-Gafford commented 6 months ago

I am trying to follow the otlp Java example here. When attempting to run the jar produced by the gradle build I am encountering the following error:

Error: Unable to initialize main class com.example.exporter.OtlpExporterExample
Caused by: java.lang.NoClassDefFoundError: io/opentelemetry/context/ImplicitContextKeyed

I have tried to run with the dependencies listed in the example gradle.build.kts file along with a few other dependencies to try to fix the error but have had no luck. Does anyone know if there are dependencies or build requirements that aren't listed in the example that I should add in my gradle.build.kts file?

Here is my gradle.build.kts for reference:

plugins {
    java
    application
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

description = "OpenTelemetry Example for OTLP Exporters"
val moduleName by extra { "io.opentelemetry.examples.otlp" }

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(8)
    }
}

dependencies {
    testImplementation(libs.junit.jupiter)
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")

    implementation(libs.guava)

    implementation(platform("io.opentelemetry:opentelemetry-bom:1.36.0"));
    implementation("io.opentelemetry:opentelemetry-api")
    implementation("io.opentelemetry:opentelemetry-sdk")
    implementation("io.opentelemetry:opentelemetry-exporter-otlp")
    implementation("io.opentelemetry:opentelemetry-context")
    implementation("io.opentelemetry:opentelemetry-sdk-common")
    implementation("io.opentelemetry.semconv:opentelemetry-semconv:1.23.1-alpha")
}

application {
    // Define the main class for the application.
    mainClass = "com.example.exporter.OtlpExporterExample"
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.example.exporter.OtlpExporterExample"
    }
}

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}
jkwatson commented 6 months ago

what does your classpath look like when you try to run the jar? The gradle build doesn't bundle all the dependencies into the jar that is built, so you'll need to make sure they are on your classpath.

how are you running the jar?

Micah-Gafford commented 6 months ago

I was able to move beyond the initial java.lang.NoClassDefFoundError by add the following to my build.gradle.kts file:

from(configurations.compileClasspath.get().map { if (it.isDirectory()) it else zipTree(it) })

I am now running into a similar issue during runtime when trying to build the OtlpGrpcSpanExporter. See here:

Exception in thread "main" java.lang.NoClassDefFoundError: io/opentelemetry/exporter/internal/marshal/Marshaler
    at com.example.exporter.ExampleConfiguration.initOpenTelemetry(ExampleConfiguration.java:43)
    at com.example.exporter.OtlpExporterExample.main(OtlpExporterExample.java:25)
Caused by: java.lang.ClassNotFoundException: io.opentelemetry.exporter.internal.marshal.Marshaler
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527)
    ... 2 more

Is this just now an issue with not having added the right implementation dependencies in my Gradle build? I am running the jar using java -jar app/build/libs/app.java.

When printing out my classpath, I get this: Classpath: app/build/libs/app.jar

jkwatson commented 6 months ago

You will need to include all of the dependencies, including any transitive dependencies in your classpath, otherwise, the classes that are needed will not be found.

jayb1976 commented 6 months ago

I am also running into a similar issue. Based on the information here, I've tried two things:

1) I've downloaded the io.opentelemetry jars listed in the build.gradle.kts and added them to my classpath 2) I've tried using the maven repository dependancies tab and to keep adding those dependancies in build.gradle.kts. I was able to get past one ClassNotFoundException just to hit another one. Here are some of the libraries I've added:

Can someone share a list of dependencies to add to build.gradle.kts or classpath?

jkwatson commented 6 months ago

Does ./gradlew run work?

jkwatson commented 6 months ago

Check out this pull request where I add the application plugin. https://github.com/open-telemetry/opentelemetry-java-examples/pull/361 With that PR in place, all you have to do is...

cd otlp
../gradlew run

(after starting up the docker-compose setup in the docker directory)

jayb1976 commented 6 months ago

@jkwatson Thank you!

I did get the project to run. However, first I had to remove

":opentelemetry-examples-spring-native", from settings.gradle.tks as that caused a build failure.

If you address that or mention something in the readme, I think this issue is resolved. Thank you again!

jkwatson commented 6 months ago

I did get the project to run. However, first I had to remove

":opentelemetry-examples-spring-native", from settings.gradle.tks as that caused a build failure.

Not sure what you're referring to there. Can you point me to the location on github?

jkwatson commented 6 months ago

I did get the project to run. However, first I had to remove ":opentelemetry-examples-spring-native", from settings.gradle.tks as that caused a build failure.

Not sure what you're referring to there. Can you point me to the location on github?

oh, in the root of the project? No idea why that would cause any failure for you. It worked fine for me as-is. Let's not confound the two issues. If you have another issue, open that as a separate one.

jayb1976 commented 6 months ago

I think that this line val moduleName by extra { "io.opentelemetry.examples.otlp" } referenced the include in settings.gradle.kts in the root of the project. The screenshot below is the error I got. Once I deleted the reference to spring-native, I re-ran ../gradlew run and it worked without issue.

image

jkwatson commented 6 months ago

Please create a separate issue for that, as it doesn't have anything to do with the running of the otlp example in particular.