Use Kotlin in VSCode

Adrian Pascu :

I am trying to get Kotlin to work inside VSCode. So far with kotlin extensions I've managed to get it working partially. I can use any Kotlin specific class or function, but I can't import any java specific class. (error: unresolved reference)

When I've compared the VSCode project to an Eclipse one and an IDEA one I've noticed that both have the JRE in the project folder (in IDEA case as an External Library). Pretty sure that is my problem in VSCode, but I don't know how to add the JRE to my project.

I am using Gradle for my project:

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
apply plugin: 'kotlin'

kotlin {
    experimental {
        coroutines 'enable'
    }
}
repositories {
    mavenCentral()
}
dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21"
}

As you can see I have the Kotlin-JVM plugin, but apart from that I am not aware of a way of adding the JRE to gradle. Can anyone please help me out?

Edit: I've tried adding kotlin coroutines to the project only to find out that even this external library won't work (I am getting the unresolved reference error both on the import kotlinx.coroutines.experimental.* and on the async function). This makes me believe that gradle isn't aware of the actual project and won't import any required dependencies.

When created the project I've used the gradle init command, modified the build.gradle file and then created a main.kt file at the root of the project (no actual code in the file, just the main function, the import statement and a call to async)

Adrian Pascu :

I've figured it out. So, what I had to do was to add the gradle application plugin and run my code with it

build.gradle

buildscript {

  ext.kotlin_version = '1.2.71'

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

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

mainClassName = 'MainKt'
defaultTasks 'run'

run{
  standardInput = System.in
}

repositories {
  mavenCentral()
}

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

And then run the code with gradle run

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=81040&siteId=1