Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

796
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda