25

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
9
  • 1
    Do you get this error when building your project with command line gradle or do you see it in Eclipse only? Commented Jun 30, 2016 at 19:14
  • As far as I know it's only eclipse. I have no problem with this using IntelliJ, but I'm trying to make this project work with eclipse and IntelliJ. Commented Jun 30, 2016 at 19:33
  • 1
    How did you build your project? I tried to build mixed-java-kotlin-hello-world and didn’t get any error. I imported project as ‘gradle’ one to eclipse, then added Kotlin nature to the project and built it using ‘Gradle tasks’. Just in case, there is no corresponding import declaration on the first screenshot, please try to add it and build project again. Commented Jun 30, 2016 at 23:03
  • I'm doing the same thing, but I'm not using tasks to build the gradle project. I can import the class by manually doing it, but my IDE should automatically be finding the classes for me if I hover over the class. What Eclipse version are you using? I'm using Neon. Commented Jun 30, 2016 at 23:31
  • I get this issue when I try to import mixed-java-kotlin-hello-world prnt.sc/bn9m4e Commented Jun 30, 2016 at 23:38

4 Answers 4

23

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']
}
Sign up to request clarification or add additional context in comments.

1 Comment

The equivalent in gradle Kotlin DSL is sourceSets { main { java.srcDir("src") kotlin.srcDirs("src/main/java","src/main/kotlin") resources.srcDir("src/main/resources") } }
11

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")
    }
}

1 Comment

Additionally, if you're writing test cases for the java classes, ensure that the test classes are in test/java/ package and not in test/kotlin package.
1

enter image description here

Placing your java sources in package like this will have same effect with Thami's answer.

Comments

0

If anyone stumbles across this thread using the Android Gradle plugin;

android {
    sourceSets {
        named("main") {
            java.srcDir("src/main/kotlin")
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.