Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, April 27, 2020

Create and Run First Kotlin Project In IntelliJ IDEA

1. Introduction


In this article, First how to create a kotlin project in IntelliJ IDEA IDE and next you'll learn how to create a class in Kotlin. Finally, running first Hello World program in Kotlin.

2. Create a Kotlin Project in IntelliJ IDEA


First, you should download the IntelliJ IDEA from jetbrains website.

download  IntelliJ IDEA


After downloading, open the IntelliJ IDEA IDE. Go to "File" menu then next click on "New" next click on "Project".


Creating koltin project file->project




Next, it displays a dialogue box. From there select Kotlin from the left sidebar and next it shows "kotlin/JVM" and "Kotlin/JS" as well as "Kotlin (Multiplatform - experimental)"

Now, Select the "Kotlin/JVM" option and click on the "Next" button.


kotlin-jvm-project


Then, it shows a dialogue box with many options such as project name, project location, project SDK, and kotlin runtime.

Provide all values as per your locations and select JDK 8 or 12 what ever you have on your machine.

Finally, Click on Finish button.

kotlin-project-details




3. Create Kotlin Class in IntelliJ IDEA


After creating the kotlin project immediately you will be seeing it on the project window.

kotlin-project-window

Here you can see the project structure.

Now select the "src" folder and right-click "New" next click on "Kotlin File/Class"

create kotlin class




Next, it shows just a small window and enter the class name "FirstKotlinApp" and select "File" from the dropdown. Next, click on Finish Button.

FirstKotlinApp-kotlin


Now, you can see the created class under src directory.

4. Run Kotlin Hello World in IntelliJ IDEA

src directory

kotlin-src-folder

Double click on the FirstKotlinApp.kt file and add the below code to the file.

[fun main(args : Array<String>){
println("Hello World. First program in kotlin.");
}]

kotlin-hello-world-program


After writing the program, save the file.

Now, Hello World program is ready and let us run it.

If you have seen the code in IntelliJ IDEA, kotlin logo is placed at the function level. Just click on the logo and then you will see the options to run the program.

run-kotlin-program

Now, It will execute the program and it produces the output as below.

[Hello World. First program in kotlin]


If you get ArrayIndexOutOfBoundsException then change the jdk to 8 or 11 then it solves the problem.

[Error:Kotlin: [Internal Error] java.lang.ArrayIndexOutOfBoundsException: 10995
at org.jetbrains.org.objectweb.asm.ClassReader.readUnsignedShort(ClassReader.java:2464)
at org.jetbrains.org.objectweb.asm.ClassReader.readUTF8(ClassReader.java:2525)
at org.jetbrains.org.objectweb.asm.ClassReader.readModule(ClassReader.java:761)
at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:646)
at org.jetbrains.org.objectweb.asm.ClassReader.accept(ClassReader.java:507)]


5. Conclusion


In conclusion, You've seen how to create and run the first hello world program in Kotlin from IntelliJ IDEA IDE.

As usual, All the examples are shown available over GitHub.

Share to see the GitHub Code.




IntelliJ IDEA Kotlin Project Setup

How to run Kotlin Project in IntelliJ IDEA.

No comments:

Post a Comment

Please do not add any spam links in the comments section.