Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin setup via gradle on eclipse

Struggling to get Kotlin running on eclipse.

I've started new graddle project. Added dependencies as prescribed on kotlin's site.

Build passes without errors.

I've created 'main.kt' file under src/java/main with:

fun main(args: Array<String>) {
    println("foo")
}

BUT, I have two problems: 1. anything from kotlin e.g. println highlighted as 'unresolved reference'. 2. I can't run a program - Error: Could not find or load main class MainKt (rightclick on main.kr run as 'kotlin application')

If I create 'new kotlin project' everything works.

my graddle build script:

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.1.2-2"
}

repositories {

    jcenter()

    mavenCentral()
}


dependencies {

    //api 'org.apache.commons:commons-math3:3.6.1'


    implementation 'com.google.guava:guava:21.0'


    testImplementation 'junit:junit:4.12'

    compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2-2"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8"

    compile "org.jetbrains.kotlin:kotlin-reflect"
    testCompile "org.jetbrains.kotlin:kotlin-test"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit"

}

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

What did I do wrong?

I've zero Java knowledge if that helps, so probably I've made some trivial error.

UPDATE:

Installed a Spring plugin and generated a new web app via it including gradle. But Kotlin behaves unpredictably there too.

At first I was not able to run it as run as Kotlin application and it errored with main could not be found, BUT sometimes it run and crashed immediately. It started to launch and crash after I've deleted and edited classes, tried creating it under other package, removing and adding Kotlin (I can't reproduce sequence to make it work again).

Fun part that gradle boot build launches everything and all works it somehow finds Kotlin's main.

Probably some issue with Kotlin plugin itself (it's load probably depends on certain events that doesn't always fire)

like image 877
ClassyPimp Avatar asked Feb 04 '23 08:02

ClassyPimp


2 Answers

Add the following to your configuration:

apply plugin: 'eclipse'
eclipse {
    classpath {
        containers 'org.jetbrains.kotlin.core.KOTLIN_CONTAINER'
    }
}

See https://gitlab.com/frnck/kotlin-gradle-eclipse for a working configuration.

like image 168
frnck Avatar answered Feb 08 '23 15:02

frnck


I'd like to add to frnck answer that this is only part of the solution. I also had to add these lines:

eclipse.project {
    buildCommand 'org.jetbrains.kotlin.ui.kotlinBuilder'
    natures 'org.jetbrains.kotlin.core.kotlinNature'
    natures 'org.eclipse.jdt.core.javanature'
    linkedResource name: 'kotlin_bin', type: '2', locationUri: 'org.jetbrains.kotlin.core.filesystem:/aio/kotlin_bin'
}
like image 26
sveri Avatar answered Feb 08 '23 14:02

sveri