Can I use kotlinx classes in Kotlin scripts?

Viewed 346

Is it possible to import classes from kotlinx package in a simple Kotlin script?

myscript.kts:

import kotlinx.serialization.*
import kotlinx.serialization.json.*

println("")

Running the above script with kotlinc -script myscript.kts gives this error:

error: unresolved reference: kotlinx

When I checked kotlinc/lib/ directory, there exists kotlinx-coroutines-core.jar and so on.
I am using Kotlin compiler version 1.4.0.

2 Answers

First of, the imports you have are related to the kotlin serialization library not the coroutines one. As for running your script you need to specify the dependencies to the compiler. You can run your script like this :

kotlinc -script myscript.kts -classpath kotlinx-serialization-runtime-0.20.0-1.4.0-rc-95.jar

I've used the old kotlin serialization runtime but you'll need to adapt the command to your needs.

Related