Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

792
Vistas
Coroutine, No Android, Module with the Main dispatcher is missing

I am trying to test Coroutine in Kotlin console project in IntellJ. I have added this library: org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.0. It worked, until I used Dispatchers.Main. After adding it, it threw a runtime exception.

import kotlinx.coroutines.*
val scope = CoroutineScope(Dispatchers.Main);
fun main(args: Array<String>) {
    scope.launch {  }
}

java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'

I switched the library with org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0 as existing answer suggested, but then it threw a different runtime error.

NoClassDefFoundError: android/os/Looper

Seems like that the library is for Android. Is using 'kotlinx-coroutines-android' the correct solution for Kotlin console project? If not, how do I fix the problem?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Dispatchers.Main is intended for GUI applications, it is not supported for the console project. As per Guide to UI programming with coroutines kotlinx.coroutines has three modules that provide coroutine context for different UI application libraries:

  • kotlinx-coroutines-android -- Dispatchers.Main context for Android applications.
  • kotlinx-coroutines-javafx -- Dispatchers.JavaFx context for JavaFX UI applications.
  • kotlinx-coroutines-swing -- Dispatchers.Swing context for Swing UI applications.

Use runBlocking to suspend main function until coroutines are executed:

fun main(args: Array<String>) = runBlocking { ... }
over 4 years ago · Santiago Trujillo Denunciar

0

Dispatchers.Main is most likely not what you want here. It is intended to dispatch coroutines on the main event loop in GUI applications.

For console applications, and especially in the kind of snippet you're sharing here, you probably instead want to use the main thread (in which main() in executed) as the event loop, and prevent main() from returning until coroutines are done. This is done by using runBlocking, which blocks the current thread for outside code, but uses it as an event loop for the CoroutineScope passed as this to the lambda that you pass to runBlocking.

So your code should likely look like:

import kotlinx.coroutines.*

fun main() {
    runBlocking { // this: CoroutineScope

        // there is a CoroutineScope here, the receiver is implicit
        launch {
            // this code will run in the main thread,
            // concurrently with other coroutines and other code
            // in the runBlocking lambda
        }
        // this code will run in the main thread,
        // concurrently with the launch and other coroutines
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

Import kotlinx-coroutines-swing:(your kotlin using version) in your dependencies gradle and compile the program

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-swing:$kotlin_coroutines_version"

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda