Mixing Java And Kotlin In Gradle Project, Kotlin Cannot Find Java Class

时光总嘲笑我的痴心妄想 提交于 2021-02-18 04:54:05

问题


As the title says, I am trying to mix Java and Kotlin in a single project. There's a good example found here. mixed-java-kotlin-hello-world. Everything is working properly besides kotlin cannot find any of my Java classes which are found in src/main/java/somepackage/SomeClass.java

What would be the cause of this?

This is the error I am getting.

When I try to edit my build.gradle, it shows I'm not spelling kotlin correctly and giving me errors on plugin but I'm copying and pasting from kotlins website.

My build.gradle looks like this

group 'Battle-OS'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.2-1"
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.2-1"
testCompile  'junit:junit:4.11'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.0.2-1"

compile 'org.slf4j:slf4j-api:1.7.18'
testCompile 'junit:junit:4.12'

    // https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'

// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '19.0'

// https://mvnrepository.com/artifact/io.netty/netty-all
compile group: 'io.netty', name: 'netty-all', version: '4.0.37.Final'

// https://mvnrepository.com/artifact/com.moandjiezana.toml/toml4j
compile group: 'com.moandjiezana.toml', name: 'toml4j', version: '0.6.0'

// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.0.2-1'

}

Instead of starting off trying to make this work on a larger project. I tried to make this work by creating a new gradle project. I created a test java class which contains a method to print hello world. Then I created a test kotlin class which creates a new object of the java class and calls the method from the java class to print hello world.

This solves the problem I had above, now I can call java classes from kotlin and kotlin from java but now it gives me errors upon running it.

Exception in thread "main" java.lang.NoClassDefFoundError:      kotlin/jvm/internal/Intrinsics
at AKotlinClassKt.main(AKotlinClass.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

回答1:


you should override sourceSets like this

sourceSets {
    main.java.srcDirs = []
    main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
    main.resources.srcDirs = ['src/main/resources']
}



回答2:


I had a similar issue and my java code was in the same source directory as the kotlin code

My solution was adding this configuration in build.gradle.kts:

configure<SourceSetContainer> {
    named("main") {
        java.srcDir("src/main/kotlin")
    }
}


来源:https://stackoverflow.com/questions/38131237/mixing-java-and-kotlin-in-gradle-project-kotlin-cannot-find-java-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!