16

当我尝试创建一个新的 OkHttpClient 对象时,抛出一个异常

我正在使用 OkHttp 3.11.0 和 OkIO 2.0.0-RC1。

线程“main”中的异常 java.lang.NoClassDefFoundError: okhttp3.ResponseBody.create(ResponseBody.java:210) 处 okhttp3.internal.Util.(Util.java:60) 处的 kotlin/TypeCastException okhttp3.OkHttpClient.(OkHttpClient. java:123) at p12.Main.main(Main.java:8) 引起:java.lang.ClassNotFoundException: kotlin.TypeCastException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader .loadClass(ClassLoader.java:424) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 4 更多

4

6 回答 6

14

您是否将 kotlin stdlib 和 stdlib-common 依赖项添加到您的构建中?看来他们不在了。

于 2018-07-28T00:50:38.063 回答
8

对我有用的解决方案(在这个github 评论中提到)是添加okhttp库本身:

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'
于 2019-10-29T16:39:29.680 回答
5

我有同样的问题。将kotlin-stdlib JAR添加到构建路径

于 2018-08-01T14:43:36.383 回答
4

您的项目很可能包含okhttpas version 的版本3.x.x,导致mockwebserver依赖于错误的版本,okhttp该版本还没有包含对 的传递依赖kotlin-stdlib-common

如果您正在使用Spring Bootmockwebserver只需将以下属性添加到您的 pom

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

于 2019-11-13T14:46:30.747 回答
1

好吧,我遇到了同样的问题,我像这样添加了 Kotlin 依赖项:

<dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-stdlib</artifactId>
   <version>1.4.32</version>
</dependency>
于 2021-04-22T14:48:36.747 回答
0

在 OSGi 中不要使用 kotlin 库,而是使用 kotlin 包: https ://kotlinlang.org/docs/reference/kotlin-osgi.html

于 2020-02-05T06:15:30.627 回答