[当我尝试创建新的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个
您是否将kotlin stdlib和stdlib通用依赖项添加到您的构建中?看起来好像缺席了。
我有同样的问题。将kotlin-stdlib JAR添加到构建路径
对我有用的解决方案(在此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'
[您的项目可能包含okhttp
版本作为3.x.x
版本,导致mockwebserver
依赖于错误的okhttp
版本,该版本也尚未包含对kotlin-stdlib-common
的传递依赖。
如果您将Spring Boot与mockwebserver一起使用,只需将以下属性添加到您的pom中>]
<okhttp3.version>4.x.x</okhttp3.version>