Okhttp: Upgrading incompatible issue: NoClassDefFoundError: kotlin/TypeCastException

Created on 17 Aug 2019  路  3Comments  路  Source: square/okhttp

Hi,

The following code snippets throw an incompatible issue when I try to upgrade MockWebServer from 3.12.0 to 4.1.0.

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import okhttp3.mockwebserver.MockWebServer;

public class DemoTest {
    private MockWebServer server;

    @Before
    public void setup() throws IOException {
        // Setup server
        server = new MockWebServer();
        server.start();
    }

    @Test
    public void test(){
        System.out.println("test");
    }
}

It throws an error:

java.lang.NoClassDefFoundError: kotlin/TypeCastException
    at com.github.jasminb.jsonapi.retrofit.DemoTest.setup(DemoTest.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
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:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 30 more

Thanks a lot.

needs info

Most helpful comment

You probably experienced the same issue as I did, which is actually not an issue of this release.
It is likely that your parent somewhere defines 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, try adding <okhttp3.version>4.1.0</okhttp3.version> to your pom.

All 3 comments

Have you included the Kotlin dependency?

You probably experienced the same issue as I did, which is actually not an issue of this release.
It is likely that your parent somewhere defines 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, try adding <okhttp3.version>4.1.0</okhttp3.version> to your pom.

The problem seems to be that mockwebserver depends on the wrong version of okhttp.

+- com.squareup.okhttp3:mockwebserver:jar:4.3.1:test
|  \- com.squareup.okhttp3:okhttp:jar:3.8.1:test
|     \- com.squareup.okio:okio:jar:1.13.0:test

If you include a current okhttp:4.3.1 there is a kotlin stdlib version clash:

+- com.squareup.okhttp3:okhttp:jar:4.3.1:test
|  +- com.squareup.okio:okio:jar:2.4.1:test
|  |  \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.50:test
|  \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.2.71:test
|     \- org.jetbrains:annotations:jar:13.0:compile

Note the kotlin-stdlib-common:1.3.50 vs. kotlin-stdlib:1.2.71.

So, in order to get a clean state you would have to do the following in maven:

    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>mockwebserver</artifactId>
      <version>${okhttp3.version}</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>com.squareup.okhttp3</groupId>
          <artifactId>okhttp</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>${okhttp3.version}</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.jetbrains.kotlin</groupId>
          <artifactId>kotlin-stdlib</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib</artifactId>
      <version>1.3.50</version>
      <scope>test</scope>
    </dependency>
+- com.squareup.okhttp3:mockwebserver:jar:4.3.1:test
+- com.squareup.okhttp3:okhttp:jar:4.3.1:test
|  \- com.squareup.okio:okio:jar:2.4.1:test
+- org.jetbrains.kotlin:kotlin-stdlib:jar:1.3.50:test
|  +- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.50:test
|  \- org.jetbrains:annotations:jar:13.0:compile

Can you please fix the versions in mockwebserver's dependencies?

Was this page helpful?
0 / 5 - 0 ratings