16

When i try to create a new OkHttpClient object an Exception get thrown

I'm using OkHttp 3.11.0 and OkIO 2.0.0-RC1.

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/TypeCastException at okhttp3.ResponseBody.create(ResponseBody.java:210) at okhttp3.internal.Util.(Util.java:60) at okhttp3.OkHttpClient.(OkHttpClient.java:123) at p12.Main.main(Main.java:8) Caused by: java.lang.ClassNotFoundException: kotlin.TypeCastException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 4 more

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Mouamle Hasan
  • 264
  • 1
  • 4
  • 14

6 Answers6

14

Did you add the kotlin stdlib and stdlib-common dependencies to your build? Looks like they're absent.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
8

The solution that worked for me (which is mentioned in this github comment) is to add okhttp library itself:

Gradle kotlin DSL:

testImplementation("com.squareup.okhttp3:okhttp:4.2.2")
testImplementation("com.squareup.okhttp3:mockwebserver:4.2.2")

Gradle Groovy DSL:

testImplementation 'com.squareup.okhttp3:okhttp:4.2.2'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.2.2'
skryvets
  • 2,808
  • 29
  • 31
5

I had the same problem. Add kotlin-stdlib JAR to the build Path

Houria
  • 71
  • 7
  • In case someone hopes to use it by adding a dependency in their `build.gradle` file, this link may be of help. https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib – Zephaniah Irvine Mar 29 '22 at 19:21
4

It is likely that your project includes the version of okhttp as version 3.x.x, causing the mockwebserver to depend on the wrong version of okhttp which also does not yet contains the transitive dependency on kotlin-stdlib-common.

If you are using Spring Boot along with mockwebserver just add the following property to your pom

<okhttp3.version>4.x.x</okhttp3.version>

Serhii Povísenko
  • 3,352
  • 1
  • 30
  • 48
1

Well, I had the same problem and I added the Kotlin dependency like this:

<dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-stdlib</artifactId>
   <version>1.4.32</version>
</dependency>
0

In OSGi do not use the kotlin libraries, but the kotlin bundle: https://kotlinlang.org/docs/reference/kotlin-osgi.html

dschulten
  • 2,994
  • 1
  • 27
  • 44