I have a runnable jar that has a UI made with javafx fxml and the class code in kotlin with some database operations done with spring jpa.
Everything works great, except that I would like to not block the main thread while doing the work. Coming from C# I saw that in kotlin you can use coroutine like async await.
import javafx.fxml.FXML
import javafx.scene.control.Button
import kotlinx.coroutines.*
class appGui{
@FXML
private var buttonExecute: Button? = null
@FXML
fun buttonExecuteClick() {
buttonExecute!!.isDisable = true
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO){
work()
}
buttonExecute!!.isDisable = false
}
}
private suspend fun work() {
println("corotine starting...")
delay(5000);
//springMain.applyChanges()
println("corotine finished...")
}
}
So I have added the coroutine call, but I got an exception Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher
Looking at this exception, I found that you have to import the coroutines-android instead of the core, so I change my pom to it
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-android</artifactId>
<version>1.4.2</version>
</dependency>
which cause other exception ClassNotFoundException: android.os.Looper Now I'm confused, is the coroutine intended to be used in android app? I'm running a jar on windows. Or do I need to go back to runnable task and do things like it was done in swing?