Tutorial: Create your first Kotlin application
In this tutorial, you will learn how to:
Create a Kotlin project.
Write code using the basic coding assistance features.
Run your code from IntelliJ IDEA.
Build and package the application.
Run the packaged application.
You can choose to build your app with one of the four supported build tools.
note
The instructions are provided for Gradle and Kotlin as DSL. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.
In IntelliJ IDEA, a project helps you organize everything that is necessary for developing your application in a single unit.
On the Welcome screen, click New Project. Alternatively, in the main menu, go to File | New | Project.
From the list on the left, select Kotlin.
Name your new project and change its location if necessary.
Select the Gradle build system.
Choose the Kotlin language for the build script.
From the JDK list, select the JDK that you want to use in your project.
If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.
If you don't have the necessary JDK on your computer, select Download JDK.
Enable the Add sample code option to create a file with a sample
Hello World!
application.Click Create.
Source code is the central part of your project. In source code, you define what your application will be doing. Real projects may be very big and contain hundreds of thousands lines of code. In this tutorial we are going to start with a simple three-line application that asks the user their name and greets them.
In the Project tool window on the left, expand the node named after your project and open the
/src file./main /kotlin /Main.kt The file only contains the
main()
function with print statements. This function is the entry point of your program.Replace the
Hello World!
sample with the following code snippet:fun main(args: Array<String>) { println("What is your name?") }
Now that the program asks users for input, provide them with a way to give it. Also, the program needs to store the input somewhere.
Move the caret to the next line and type
val name = rl
. IntelliJ IDEA will suggest to convertrl
toreadln()
. Press Enter to accept the suggestion.Move the caret to the next line, type
sout
, and press Enter.tip
This feature is called live templates. For more information about other available live templates or configure your own, go to Settings | Editor | Live Templates | Kotlin.
Place the caret inside the parentheses of the
println
statement and type"Hello, $"
. Press CtrlSpace to invoke code completion and select thename
variable from the list.
Now we have a working code that reads the username from the console, stores it in a read-only variable, and outputs a greeting using the stored value.
Let's verify that our program works as expected.
IntelliJ IDEA allows you to run applications right from the editor. You don't need to worry about the technical aspect because IntelliJ IDEA automatically does all the necessary preparations behind the scenes.
Click the Run icon in the gutter and select Run 'MainKt' or press CtrlShiftF10.
When the program has started, the Run tool window opens, where you can review the output and interact with the program.
![The output of the program in the Console tab The output of the program in the Console tab](https://resources.jetbrains.com/help/img/idea/2024.3/kotlin_create_first_app_run_2.png)
At this point, you know how to write code and run it from IntelliJ IDEA, which is convenient in the development process. However, this is not the way the users are supposed to run applications. For the users to run it on their computers, we are going to build and package the application as a jar file.
tip
JAR is a file format for distributing applications as a single file.
Building the app includes the following steps:
Compiling the sources – in this step, you translate the code you've just written into JVM bytecode. The compiled sources have the .class extension.
Bundling the dependencies – for the application to function correctly, we need to provide the libraries it depends on. The only required library for this project is Kotlin runtime, but still, it needs to be included. Otherwise, the users will have to provide it themselves every time they run the program, which is not very convenient.
Both the compiled sources and the dependencies end up in the resulting .jar file. The result of the build process such as .jar file is called an artifact.
note
The instructions are provided for Gradle and Kotlin as DSL. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.
Open the
build.gradle.kts
script.tip
The build script is the file that tells the build tool how exactly to build the project. It is written in Kotlin just like the source code of your program.
In the build script, add the following task definition:
tasks.jar.configure { manifest { attributes(mapOf("Main-Class" to "org.example.MainKt")) } configurations["compileClasspath"].forEach { file: File -> from(zipTree(file.absoluteFile)) } duplicatesStrategy = DuplicatesStrategy.INCLUDE }
The
manifest
section specifies the entry point of the program. When you set"Main-Class"
to"org.example.MainKt"
, you're telling the JAR file that"org.example.MainKt"
is the entry point, indicating that theMainKt
class is in theorg.example
package. This is necessary to make the JAR file executable.The rest of the code tells the build tool to recursively scan the project for dependencies and include them in the build.
When the definition is added to the build file, press CtrlShift0O or click
in the Gradle tool window to import the changes.
In the right-hand sidebar, open Gradle and run the
jar
task (Tasks | build | jar). If the sidebar is not present, go to View | Appearance in the main menu and toggle the Tool Window Bars menu option on.
The resulting JAR appears in the build/libs directory.
![The resulting JAR in the out directory viewed in the Project Tool Window The resulting JAR in the out directory viewed in the Project Tool Window](https://resources.jetbrains.com/help/img/idea/2024.3/kotlin_tutorial_gradle_jar_2.png)
In the Project tool window, right-click the .jar file and select Run.
This is a quick way to run a .jar file from IntelliJ IDEA.
Open the terminal and from the containing folder, run:
java -jar greeting-1.0-SNAPSHOT.jar
This is how the users of your application would run it.
note
JDK 1.8 or later is required to run the .jar
Thanks for your feedback!