Install IntelliJ IDEA Community Edition and Write Hello Kotlin Program

To write Kotlin program you will need an IDE (Integrated Development Environment). One of the IDE that you can use is the IntelliJ IDEA CE (Community Edition). It comes with Kotlin Java Runtime Library, so you don’t need to install it separately.

To run Kotlin program you will need to install the latest JDK (Java Development Kit) which can be downloaded freely from the Java Download Website. Download version for your operating system and run the installer.

To check if Java successfully installed you can type the following command in your terminal or command prompt:

java -version

If you see something this then Java Development Kit is installed in your system.

java version "17" 2021-09-14 LTS
Java(TM) SE Runtime Environment (build 17+35-LTS-2724)
Java HotSpot(TM) 64-Bit Server VM (build 17+35-LTS-2724, mixed mode, sharing)

After installing the JDK you can download the IntelliJ IDEA CE. Double-click the installer to install it. For more details on installation and set up you can check out this website.

Now let’s create our first simple Kotlin program in IntelliJ IDEA.

  • Open the IntelliJ IDEA CE and click the Create New Project from the Welcome Screen dialog.
Create New Kotlin Project

Create New Kotlin Project

  • On the New Project dialog choose Kotlin from the left sidebar and then choose Kotlin/JVM in the selection on the right sidebar. Press the Next button.
New Kotlin Project Setup

New Kotlin Project Setup

  • Enter the project name Hello Kotlin, the project location path and the JDK will be used in the project.
New Kotlin Project Configurations

New Kotlin Project Configurations

  • Click the Finish button to create the project. It will bring you to the following screen.
HelloKotlin Project in IntelliJ IDEA CE

HelloKotlin Project in IntelliJ IDEA CE

  • Right-click on the src directory and choose NewKotlin File/Class from the menu. Enter HelloKotlin in the File/Class name.
New Kotlin File/Class Dialog

New Kotlin File/Class Dialog

  • In the HelloKotlin.kt type in the following code snippet.
fun main(args: Array<String>) {
    if (args.isEmpty()) {
        println("Hello, World!")
        return
    } else {
        println("Hi, hello ${args[0]}!")
    }
}
  • To run it, right-click on the editor and choose the Run HelloKotlinKt from the menu.
  • If you want to run it with an argument, you can set it in the Run/Debug Configurations dialog.
  • To open the Run/Debug Configurations click the down-arrow button next to HelloKotlinKt in the navigation bar on the top-right and choose Edit Configurations…
  • Type in the arguments in the Program arguments textbox.
Run/Debug Configurations

Run/Debug Configurations

That’s all. Now you have your JDK, IntelliJ IDEA CE installed and created your first Kotlin program. If you have any questions, please post it in the comments section below. Thank you and have fun with Kotlin.

How do I write Hello World in Kotlin?

In Kotlin, the HelloWorld.kt program can be written as a simple function like the following snippet.

fun main(args: Array<String>) {
    println("Hello, World!")
}

From this little code snippet we can learn the following features of the Kotlin programming language:

  • Kotlin program saved in a file with .kt extension.
  • The fun keyword to declare a function to show you that programming can be fun again 😉
  • To declare variables we start with the variable name followed by its type separated by a colon.
  • We don’t have to end a statement with a semicolons.
  • To print we can use the println function, which is a wrapper to Java System.out.println.
  • The main function is the execution entry point of our HelloWorld program.
  • We can create functions at the top level file without creating a class.

To compile the HelloWorld.kt program we run the following command:

kotlinc HelloWorld.kt

The compiler creates a class file called HelloWorldKt.class. To run it type the following command, assumed that you’ve set up the $KOTLIN_LIB environment variable. In my case the variable is set to /usr/local/Cellar/kotlin/1.2.40/libexec/lib.

java -cp $KOTLIN_LIB/kotlin-stdlib.jar:. HelloWorldKt