Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Programming Cookbook

You're reading from   Kotlin Programming Cookbook Explore more than 100 recipes that show how to build robust mobile and web applications with Kotlin, Spring Boot, and Android

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher
ISBN-13 9781788472142
Length 434 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Aanand Shekhar Roy Aanand Shekhar Roy
Author Profile Icon Aanand Shekhar Roy
Aanand Shekhar Roy
Rashi Karanpuria Rashi Karanpuria
Author Profile Icon Rashi Karanpuria
Rashi Karanpuria
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Installation and Working with Environment 2. Control Flow FREE CHAPTER 3. Classes and Objects 4. Functions 5. Object-Oriented Programming 6. Collections Framework 7. Handling File Operations in Kotlin 8. Anko Commons and Extension Function 9. Anko Layouts 10. Databases and Dependency Injection 11. Networking and Concurrency 12. Lambdas and Delegates 13. Testing 14. Web Services with Kotlin 15. Other Books You May Enjoy

How to run a Kotlin compiled class

Working with the command-line compiler for any language is one of the first steps to get a better understanding of the language, and this knowledge comes handy at a lot of times. In this recipe, we will run a Kotlin program using the command line, and we will also play a bit with the interactive shell of Kotlin.

Getting ready

To be able to perform this recipe, you need a Kotlin compiler installed on your development machine. Every Kotlin release ships with a standalone compiler. You can find the latest release at https://github.com/JetBrains/kotlin/releases.

To manually install the compiler, unzip the standalone compiler into a directory and optionally, add the bin directory to the system path. The bin directory contains the scripts needed to compile and run Kotlin on Windows, OS X, and Linux.

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:

How it works...

The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it. Then, we use Java to run the .jar file generated.

The -d option in the command indicates what we want the output of the compiler to be called and maybe either a directory name for class files or a .jar filename.

There's more...

Kotlin can also be used for writing shell scripts. A shell script has top-level executable code.

Kotlin script files have the .kts extension as opposed to the usual .kt for Kotlin applications.

To run a script file, just pass the -script option to the compiler:

$ kotlinc -script kotlin_script_file_example.kts
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image