5

I am new to Kotlin and trying to learn asynchronous programming using coroutine. I am following Kotlin official docs. But when I tried to compile my code, it showed me error: "unresolved reference: kotlinx". So I just want to know, how can I use Coroutine in non android projects?

I am using Ubuntu terminal to compile the code.

Code Snippet

import kotlinx.coroutines.*  
 fun main(args: Array<String>){     
     GlobalScope.launch{ 
       delay(1000L)
       println("World!")   
     }
     println("Hello,") 
     Thread.sleep(2000L)
 }
hippietrail
  • 15,848
  • 18
  • 99
  • 158

1 Answers1

3

Download kotlinx-coroutines-core-1.2.1.jar, put it into the same folder where the source file (Example.kt) is, and compile it:

kotlinc Example.kt -cp kotlinx-coroutines-core-1.2.1.jar -include-runtime -d Example.jar

Run Example.jar using command

java -cp kotlinx-coroutines-core-1.2.1.jar:Example.jar ExampleKt
atarasenko
  • 1,768
  • 14
  • 19
  • 2
    I'm too much of a newb at Kotlin and Java to figure out how to download from that page (`No direct downloads selected for this package.`). Much easier for me was [this one on mvnrepository](https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.3.0) – hippietrail Sep 01 '19 at 05:04