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.
- 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.
- Enter the project name Hello Kotlin, the project location path and the JDK will be used in the project.
- Click the Finish button to create the project. It will bring you to the following screen.
- Right-click on the
src
directory and choose New ▶ Kotlin File/Class from the menu. EnterHelloKotlin
in the File/Class name.
- 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.
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.