6

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)

7
  • You don't add the JRE to gradle. Set the JAVA_HOME variable globally and gradle should be able to find it. If you are running Linux, this should be done by your distro when installing Java, but otherwise /etc/profile is a good place to define the variable. Otherwise, I recommend changing the OS on the long run and use Google to find a temporary solution Commented Sep 30, 2018 at 17:01
  • I am on windows. I've set up the variable, but it still won't let me import the java class Commented Sep 30, 2018 at 18:06
  • @msrd0 I've edited the post adding a couple extra information. If you are kind enough, reread it Commented Sep 30, 2018 at 18:15
  • Sorry, no idea. I've only ever used Idea & Atom on Linux. If gradle init works this means gradle can find the JRE (gradle cannot launch without it). Try gradle assemble to see whether your problem is with gradle or with VSCode Commented Sep 30, 2018 at 18:34
  • @msrd0 The command worked. Also, when I've added the coroutine, VSCode syntax check would mark the call to async as an error, but after a restart that syntax error is gone and I even get the documentation reference, so is not from VSCode. I am thinking I am running the code with the wrong command or I need some specific folder structure. If you've used Atom,what command did you use to build the project? Commented Sep 30, 2018 at 18:46

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.