19

I am trying to use kotlin with gradle, but I am unable to succesfully create a project with Intellij Idea 15.

I've create simple project with two modules hello-java and hello-kotlin.

hello-java is plain java project and it is compiling and running perfectly fine.

hello-kotlin is simple kotin module, with just one *.kt file and build.gradle file.
Here are the sources:

build.gradle

group 'pl.fzymek.kotlin'
version '1.0-SNAPSHOT'

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

apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = 'HelloKotlinKt'

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:0.10.4"
}

HelloKotlin.kt

fun main(args: Array<String>) {
    println("Hello, Kotlin!")
}

main module settings.gradle

include 'hello-java'
include 'hello-kotlin'

When running gradlew clean build all projects are compiled successfully, but when running java -jar hello-kotlin-1.0-SNAPSHOT.jar I get following error:

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at HelloKotlinKt.main(HelloKotlin.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

What is more, Intellij seems not to recognize src/main/kotlin directory as source directory (it's not marked in blue) and I am not able to use auto-complete feature when editing HelloKotlin.kt file.

Here's my project structure in Intellij project window

enter image description here

I've tried using Intellij option to configure modules with Kotlin(Tools->Kotlin->Configure project with Kotlin), but it gives me error that "All modules with kotlin files are configured"

Help me stackoverflow, you are my only help.

Filip Zymek
  • 2,626
  • 4
  • 22
  • 31
  • 1
    Why are you using Kotlin 0.10? The latest version is 1.0-beta. – yole Nov 16 '15 at 15:41
  • No particular reason. I've tried different versions. I thought my problem has something to do with kotlin version. – Filip Zymek Nov 16 '15 at 16:18
  • Well, the stacktrace you have shown contains a class that no longer exists in the latest Kotlin version, so yes, it has something to do with Kotlin version. Please do always use the latest one. – yole Nov 16 '15 at 17:38

3 Answers3

12

When running gradlew clean build all projects are compiled successfully, but when running java -jar hello-kotlin-1.0-SNAPSHOT.jar I get following error...

jar {
    manifest {
        attributes 'Main-Class': 'HelloKotlinKt'
    }

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

What is more, Intellij seems not to recognize src/main/kotlin directory as source directory (it's not marked in blue)...

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

Documentation and useful resources can be found here and there.

Community
  • 1
  • 1
aga
  • 27,954
  • 13
  • 86
  • 121
  • According to doc, 'sourceSets' should be updated when not using default dir structure, but this is not my case. I am using default one so I think this does not make sense to add it in 'build.gradle' – Filip Zymek Nov 16 '15 at 22:08
  • 1
    Main-Class attribute in your answer would not work because Kotlin wouldn't generate a class named `HelloKotlin.kt`. The correct name is `HelloKotlinKt`. – Alexander Udalov Nov 17 '15 at 09:44
  • @AlexanderUdalov you're absolutely right, thanks for comment. :) – aga Nov 17 '15 at 09:49
4

Why doesn't my application run?

The Jar you're creating doesn't include the kotlin runtime as Gradle will only build a Jar with your class files in it. I see you're using the application plugin so either doing $ gradle run or creating a distribution and executing through the provided shell script should work ok. If you want to ship kotlin with your Jar you'll need to create a fat jar.

Why doesn't IDEA recognise the source directory?

I suspect this is down to the fact you haven't applied the idea plugin in your build file. I haven't done any work with Kotlin, but with other languages this is required to set up the workspace correctly.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • I just tried applying `idea` plugin, but it seems to have no effect. I've rebuilded project and even restarted IDE but nothing has changed. – Filip Zymek Nov 16 '15 at 15:10
  • How are you importing/building your project? – tddmonkey Nov 16 '15 at 15:14
  • I've taken your `build.gradle` file, applied the idea plugin, run `$ gradle idea` and the resulting project has `src/main/kotlin` marked as a source directory – tddmonkey Nov 16 '15 at 15:21
  • I run gradlew clean build to generate jars. I'll try your solution when I get near my pc :) – Filip Zymek Nov 16 '15 at 16:20
  • I've deleted all intellij and gradle dirs and files and reimported project. Now `src/main/kotlin` is recognized properly. After adding `from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }` as @aga suggested, jar file can be run. – Filip Zymek Nov 17 '15 at 08:28
0

I just do that documentation says, and all work fine. "Kotlin sources can be mixed with Java sources in the same folder, or in different folders. The default convention is using different folders:

project
    - src
        - main (root)
            - kotlin
            - java

The corresponding sourceSets property should be updated if not using the default convention:

sourceSets {
    main.kotlin.srcDirs += 'src/main/myKotlin'
    main.java.srcDirs += 'src/main/myJava'
}

Hope it helps for you.