konrad-kaminski / spring-kotlin-coroutine

Kotlin coroutine support for Spring.
448 stars 69 forks source link

java.lang.ClassNotFoundException: kotlinx.coroutines.experimental.Dispatchers #36

Closed VikaChebotar closed 5 years ago

VikaChebotar commented 5 years ago

Hi! When I add "@EnableCoroutine" to my Application class, build fails with exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DefaultDispatcher' defined in org.springframework.kotlin.experimental.coroutine.context.CoroutineContexts: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [kotlinx.coroutines.experimental.CoroutineDispatcher]: Factory method 'defaultDispatcher' threw exception; nested exception is java.lang.NoClassDefFoundError: kotlinx/coroutines/experimental/Dispatchers at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] at com.mydictionary.ApplicationKt.main(Application.kt:22) [classes/:na] Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [kotlinx.coroutines.experimental.CoroutineDispatcher]: Factory method 'defaultDispatcher' threw exception; nested exception is java.lang.NoClassDefFoundError: kotlinx/coroutines/experimental/Dispatchers at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] ... 18 common frames omitted Caused by: java.lang.NoClassDefFoundError: kotlinx/coroutines/experimental/Dispatchers at org.springframework.kotlin.experimental.coroutine.context.CoroutineContexts.defaultDispatcher(CoroutineContexts.kt:29) ~[spring-kotlin-coroutine-0.3.6.jar:na] at org.springframework.kotlin.experimental.coroutine.context.CoroutineContexts$$EnhancerBySpringCGLIB$$8b78d0d0.CGLIB$defaultDispatcher$1(<generated>) ~[spring-kotlin-coroutine-0.3.6.jar:na] at org.springframework.kotlin.experimental.coroutine.context.CoroutineContexts$$EnhancerBySpringCGLIB$$8b78d0d0$$FastClassBySpringCGLIB$$cba81175.invoke(<generated>) ~[spring-kotlin-coroutine-0.3.6.jar:na] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.kotlin.experimental.coroutine.context.CoroutineContexts$$EnhancerBySpringCGLIB$$8b78d0d0.defaultDispatcher(<generated>) ~[spring-kotlin-coroutine-0.3.6.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] ... 19 common frames omitted Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.experimental.Dispatchers at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_181] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181] ... 30 common frames omitted

What's wrong with my configuration?

buildscript {
    ext {
        kotlinVersion = '1.2.70'
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}

repositories {
    maven { url 'https://dl.bintray.com/konrad-kaminski/maven' }
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
kotlin {
    experimental {
        coroutines 'enable'
    }
}
dependencies {
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation "org.jetbrains.kotlin:kotlin-reflect"
    implementation "com.google.firebase:firebase-admin:6.4.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21"
    implementation "org.springframework.kotlin:spring-kotlin-coroutine:0.3.6"
    implementation "org.springframework.kotlin:spring-webmvc-kotlin-coroutine:0.3.6"
    testimplementation 'org.springframework.boot:spring-boot-starter-test'
    testimplementation group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
task wrapper(type: Wrapper) {
    gradleVersion = '2.2.1'
}
task stage(dependsOn: ['build', 'clean'])
build.mustRunAfter clean
r6q commented 5 years ago

The problem itself is sort of irrelevant to spring-kotlin-coroutine project. This is a common dependency management issue. What give it out is java.lang.NoClassDefFoundError If you ever see this issue, most likely one of your dependencies has incorrect version. In your particular case it is kotlinx/coroutines/experimental/Dispatchers as you are using implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" You should be using 0.26.0 where this class exists.

VikaChebotar commented 5 years ago

@robertt1234 Thank you! This helped