Kotlin Command Line Compiler

In Android development I occasionally need test some separated functionality. Building the whole project takes time and may causes side-effects. That’s why I use command line compiler.

Download the compiler from Github (i.e. kotlin-compiler-1.3.70.zip). In bin directory there are kotlin and kotlinc files that we will use. Create some source code file named RxKotlinTest.kt with this content:

import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.rxkotlin.toObservable

fun main(args: Array<String>) {
    listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
        .toObservable()
        .filter { it.length >= 5 }
        .subscribeBy(
            onNext = { println(it) },
            onError =  { it.printStackTrace() },
            onComplete = { println("Done!") }
        )
}

Then compile it to .jar file and run it. It’s necessary to link all used libraries. You can download them from search.maven.org.

kotlinc RxKotlinTest.kt -cp rxjava-2.2.19.jar:rxkotlin-2.4.0.jar -d rx-kotlin-test.jar
kotlin -cp rx-kotlin-test.jar:reactive-streams-1.0.3.jar:rxjava-2.2.19.jar:rxkotlin-2.4.0.jar RxKotlinTestKt

Note: If you run kotlin compiler without parameters, REPL starts. REPL (Read-Eval-Print-Loop) is interactive shell where you can type commands and you see a result.

Leave a Reply

Your email address will not be published.