question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compile Kotlin with coroutines via command line

See original GitHub issue

STRUCTURE:

1.Main.kt file

import kotlinx.coroutines.*

fun main(args: Array<String>) {
	GlobalScope.launch { // launch a new coroutine in background and continue
		delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
		println("World!") // print after delay
	}
	println("Hello,") // main thread continues while coroutine is delayed
	Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}

2. kotlinx-coroutines-core-1.3.0-M1.jar https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.3.0-M1

COMPILE COMMAND: kotlinc Main.kt -cp kotlinx-coroutines-core-1.3.0-M1.jar -include-runtime -d program.jar output: program.jar

RUN: java -jar program.jar

EXCEPTION:

Exception in thread “main” java.lang.NoClassDefFoundError: kotlinx/coroutines/GlobalScope at MainKt.main(Main.kt:10) Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.GlobalScope at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) … 1 more


How can I fix this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
elizarovcommented, Jun 18, 2019

You should also specify additional classpath libraries when you run it:

java -cp kotlinx-coroutines-core-1.3.0-M1.jar:program.jar MainKt
0reactions
patitowcommented, May 31, 2022

There is any way to use coroutines compiled via command line using kotlinc-js ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin command-line compiler
Create a simple application in Kotlin that displays "Hello, World!" . In your favorite editor, create a new file called hello.kt with the...
Read more >
How to use Kotlinc coroutine in non android project?
I am using Ubuntu terminal to compile the code. Code Snippet import kotlinx.coroutines.* fun main(args: Array<String> ...
Read more >
How to Write a Command-Line Tool with Kotlin Multiplatform
A starter project to build command-line tools in Kotlin Multiplatform Contains a re-implementation of a real world CLI… ... If you build something ......
Read more >
Install, compile and run Kotlin from command line - Codexpedia
Compile multiple Kotlin files. Put all the Kotlin files between kotlinc and -include-runtime, or use the wildcard (*.kt) to include all Kotlin files...
Read more >
Kotlin 1.3 Released with Coroutines, Kotlin/Native Beta, and ...
In Kotlin 1.3 coroutines graduated to stable, making non-blocking code easy ... The command-line compiler can be downloaded from the Github ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found