Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

kotlinx.serialization - Can I use kotlinx classes in Kotlin scripts?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...