Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Last active April 22, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hastebrot/62a7a4765d771ce942d095a3b495ca4a to your computer and use it in GitHub Desktop.
Save hastebrot/62a7a4765d771ce942d095a3b495ca4a to your computer and use it in GitHub Desktop.

Mini Guide: kscript and kotlinx.coroutines

$ brew install holgerbrandl/tap/kscript
$ brew install maven
$ kscript demo.kts
Hello from Kotlin!
Hello,
World!

demo.kts:

#!/usr/bin/env kscript

@file:MavenRepository("central", "https://repo.maven.apache.org/maven2/")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:0.22.5")

import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch

println("Hello from Kotlin!")

launch {
    delay(1000L)
    println("World!")
}
println("Hello,")
Thread.sleep(2000L)

Links:

Dependency Repositories:

Caveats:

kscript requires maven to resolve dependencies.

$ kscript demo.kts
[kscript] Resolving dependencies...[kscript] [ERROR] mvn is not in PATH

$ brew install maven

when resolved dependencies were removed, the kscript cache needs to be deleted.

$ kscript demo.kts
$ rm -r ~/.m2/repository/org/jetbrains/kotlinx/
$ kscript demo.kts
...
Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.experimental.BuildersKt

$ rm -r ~/.kscript/

dependencies can also be downloaded manually.

$ mvn dependency:get -Dartifact=org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment