Kotlin Programming Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Now we are ready to run our first program using the command line. First, we will create a simple application that displays Hello World! and then compile it:

  1. Create a file with the name hello.kt and add the following lines of code in that file:
fun main(args: Array<String>) {
println("Hello, World!")
}
  1. Now we compile the file using the following command:
$ kotlinc hello.kt -include-runtime -d hello.jar
  1. Now we run the application using the following command:
$ java -jar hello.jar
  1. Suppose you want to create a library that can be used with other Kotlin applications; we can simply compile the Kotlin application in question into .jar executable without the -include-runtime option, that is, the new command will be as follows:
$ kotlinc hello.kt -d hello.jar
  1. Now, let's check out the Kotlin interactive shell. Just run the Kotlin compiler without any parameters to have an interactive shell. Here's how it looks:

Hopefully, you must have noticed the information I am always guilty of ignoring, that is, the command to quit interactive shell is :quit and for help, it is :help.

You can run any valid Kotlin code in the interactive shell. For example, try some of the following commands:

  • 3*2+(55/5)
  • println("yo")
  • println("check this out ${3+4}")

Here's a screenshot of running the preceding code: