Search code examples
javagradlekotlin

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


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. enter image description here

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. enter image description here

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.

enter image description here

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

Solution

  • 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']
    }