[JAVA] How to run a Kotlin Coroutine sample from the command line

Thing you want to do

Build and run the following code that everyone runs first on the command line.


import kotlinx.coroutines.*

fun main() {
    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
}

Prerequisites

Work to be carried out

  1. Download kotlin compiler
  2. Download the kotlin coroutine jar file
  3. Compile source code
  4. Run from command line

procedure

  1. Download kotlin compiler

The procedure is described on the official website (here)

  1. Download the kotlin coroutine jar file

When you execute the following command, kotlinx-coroutines-core-1.3.5.jar will be downloaded to the current directory.


mvn dependency:get -Dartifact=org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5  -Ddest=./
  1. Compile source code

When you execute the following command First.jar will be generated in the current directory.

kotlinc -cp kotlinx-coroutines-core-1.3.5.jar  first.kt -include-runtime -d first.jar
  1. Run from command line

Execute the following command

java -cp ./first.jar:./kotlinx-coroutines-core-1.3.5.jar FirstKt

Why did you do this?

The reasons are the following three points

-When I run it on Playground, Hello and World are displayed at the same time, so I thought it was subtle. --I wanted to build the kotlin source using the extension library from the command line. --It was troublesome to launch Android Studio

Recommended Posts

How to run a Kotlin Coroutine sample from the command line
How to run a GIF file from the Linux command line (Ubuntu)
How to download and run a Jar package directly from the Maven repository with just the command line
How to apply C code format from the command line
How to run the SpringBoot app as a service
Learn how to customize the Navigation Bar from the sample app
To use the "java" command line tool ... How to avoid popping up
How to return a value from Model to Controller using the [Swift5] protocol
Using JUnit from the command line on Ubuntu
How to get a heapdump from a Docker container
[Swift] How to implement the LINE login function
How to delete a controller etc. using a command
How to run a djUnit task in Ant
Compile and run Java on the command line
How to create a route directly from the URL you want to specify + α
3. Create a database to access from the web module
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
How to deploy to Heroku from a local docker image
[Java] How to extract the file name from the path
How to launch another command in a Ruby program
[Java] How to erase a specific character from a character string
How to determine the look-ahead request (Prefetch) from the browser
Notes on updating Ubuntu from the command line (16.04 → 18.04, 18.04 → 20.04 both)
Kotlin may take the world from App to Web
How to make a mod for Slay the Spire
Send a command to Spigot from an external process
[Java] How to start a new line with StringBuilder
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
How to transition from the [Swift5] app to the iPhone settings screen
SDWebImage: How to clear the cache for a particular UIImageView
How to write a migration from Rails datetime type to date type
[IOS] How to get the table name from AWS DynamoDB
How to create a placeholder part to use in the IN clause
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
How to get the longest information from Twitter as of 12/12/2016
How to Burning a Install Disk of Windows from Ubuntu
How to disable Set-Cookie from API on the front side
How to deploy a kotlin (java) app on AWS fargate
[Ruby] How to retrieve the contents of a double hash
How to add the same Indexes in a nested array
Run the sample "Introduction to TensorFlow Development" on Jetson nano
How to run React and Rails on the same server
[jsoup] How to get the full amount of a document
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to leave a comment
Decomposing the Docker run command. .. ..
How to migrate from JUnit4 to JUnit5
How to insert a video
How to create a method
How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8
How to run a job with docker login in AWS batch
Install lsb_release from the command line when lsb_release fails in docker environment
[Rails] How to put a crown mark on the ranking function
How to store the information entered in textarea in a variable in the method
How to open a script file from Ubuntu with VS code
How to identify the path that is easy to make a mistake
Try to make a CS 3D tile from the Geographical Survey tile
[chown] How to change the owner of a file or directory