kotlin / TypeCastException尝试创建OkHttpClient对象时

问题描述 投票:11回答:4

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

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

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

java okhttp okio
4个回答
10
投票

您是否将kotlin stdlib和stdlib通用依赖项添加到您的构建中?看起来好像缺席了。


5
投票

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


2
投票

对我有用的解决方案(在此github comment中提到)是添加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'

2
投票

[您的项目可能包含okhttp版本作为3.x.x版本,导致mockwebserver依赖于错误的okhttp版本,该版本也尚未包含对kotlin-stdlib-common的传递依赖。

如果您将Spring Boot与mockwebserver一起使用,只需将以下属性添加到您的pom中>]

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

© www.soinside.com 2019 - 2024. All rights reserved.