Kotlin setup via gradle on eclipse

Viewed 4267

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)

5 Answers

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

For Eclipse 2018-12 and kotlin 1.3 the solution was a combination of other answers plus some additional settings file:

eclipse {
    classpath {
        //Adds the kotlin container to the classpath
        containers 'org.jetbrains.kotlin.core.KOTLIN_CONTAINER'
        //Fixes the right output path
        defaultOutputDir = file('bin')
        //Make all src folders output in the same output folder (default)
        file {
            whenMerged { 
                // use default Output for all source-folders. see also defaultOutputDir per project
                entries.each { source ->
                    // only Source-folders in the project starting with '/' are project-references
                    if (source.kind == 'src' && !source.path.startsWith('/')) { 
                        source.output = null
                    }
                }
            }
        }
    }


    project{
        buildCommand 'org.jetbrains.kotlin.ui.kotlinBuilder'
        //Fixes the natures
        natures 'org.jetbrains.kotlin.core.kotlinNature'
        natures 'org.eclipse.jdt.core.javanature'
        //Links the kotlin_bin folder (generated class files)
        linkedResource name: 'kotlin_bin', type: '2', locationUri: "org.jetbrains.kotlin.core.filesystem:/${project.name}/kotlin_bin".toString()
        file{
            whenMerged{

                def kotlinPrefs = file('.settings/org.jetbrains.kotlin.core.prefs')
                def jdkHome = System.properties.'java.home'
                if(!(jdkHome)){
                    throw new GradleException('No JDK home available for setting up Eclipse Kotlin plugin, setup env "java.home" or update this script.')
                }
                kotlinPrefs.write """\
                    codeStyle/codeStyleId=KOTLIN_OFFICIAL
                    codeStyle/globalsOverridden=true
                    compilerPlugins/jpa/active=true
                    compilerPlugins/no-arg/active=true
                    compilerPlugins/spring/active=true
                    eclipse.preferences.version=1
                    globalsOverridden=true
                    jdkHome=$jdkHome
                """.stripIndent()
            }
        }
    }
}

I would like to add to Felipe Nascimento's answer that the location of the .settings folder does not yet exist. It works when the line below is inserted into that answer.

def kotlinPrefs = file("../${project.name}/.settings/org.jetbrains.kotlin.core.prefs".toString())

I have found that the JAVA_HOME environment variable that is set when your run this task ;

gradle cleanEclipse eclipse

is the one that is included in the Eclipse BuildPath

Related