ZOFTINO.COM android and web dev tutorials

Running Kotlin Program in IntelliJ IDEA

You can use IntelliJ IDEA to develop kotlin applications. This tutorial explains steps involved in creating and running Kotlin application using IntelliJ IDEA.

First download and install latest JDK. Then download IntelliJ from Jetbrains and extract zip or run exe. Following image shows list of directories and file in the directory where intellij is installed.

intellij installation

Then open IntelliJ by clicking idea file in bin folder. It will open IntelliJ welcome screen, on that screen, click create new project.

intellij create project

In the new project window, select kotlin and kotlin/JVM to create kotlin jvm prject. Then click next.

kotlin intellij new project

In the next window, you need to enter project name, location and select JDK. Then click finish.

kotlin intellij new project name location sdk

That will create project with project structure shown in the following image.

intellij kotlin project structure

To add Kotlin code, you need to create Kotlin file. To do that, right click src, click new and then click kotlin file. Then next window will open, there you need to enter file name and click ok to create a kotlin file.

intellij create kotlin file

Then in the Kotlin file, type main and hit ctrl-space to get auto complete suggestions. Select main function suggestion to add main routine to the file.

intellij add kotlin main function

Then add code in the main function. To run kotlin program in intellij, you need to click Run menu and then click run.

Following is the Kotlin sample program which prompts user to enter two numbers, then it will calculate sum and display it on the console.

import java.util.*

fun main(args: Array<String>) {
    println("Enter first number: ")
    val input = Scanner(System.`in`)
    val numOne = input.nextInt()
    println("Enter second number: ")
    val numTwo = input.nextInt()
    val sum = numOne+numTwo
    println("Enter second number: $sum")
}

Output

Enter first number: 
22
Enter second number: 
33
Enter second number: 55