# Tutorial: Create your first Kotlin application

> **TL;DR**
> Required plugin: Kotlin (bundled)

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.

> **Note:**
> The instructions are provided for Gradle and Groovy as DSL. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.

> **Note:**
> The instructions are provided for Maven. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.

> **Note:**
> The instructions are provided for IntelliJ build tool. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.

## Create a new project

In IntelliJ IDEA, a project helps you organize everything that is necessary for developing your application in a single unit.

Procedure:

1. On the Welcome screen, click New Project. Alternatively, in the main menu, go to `File | New | Project`.

2. From the list on the left, select Kotlin.

3. Name your new project and change its location if necessary.

4. Select the Gradle IntelliJ Maven build system.

> **Tip:**
> If you want to see instructions for another build system for the tutorial, select it in the Build tool switcher at the top of this page.

5. Choose the Groovy Kotlin language for the build script.

6. From the JDK  list, select the [JDK](sdk.html#jdk) that you want to use in your project.

* If the JDK is installed on your computer, but not defined in the IDE, select ![](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.nodes.jdk.png) Add JDK from Disk… and specify the path to the JDK home directory.

* If you don't have the necessary JDK on your computer, select ![](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.general.download.svg) Download JDK .

> **Tip:**
> If you don't know which distribution to choose, and you don't have specific requirements that instruct you to use one of the existing distributions, use Oracle OpenJDK.

For more information about JDK and how to install it, see the [Java Development
Kit
(JDK) guide](sdk.html#jdk).

7. Make sure the Add sample code option is enabled. It will create a file with a sample `Hello World!` application.

![New Kotlin project with the Gradle build system with Kotlin DSL](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin-new-project-gradlek.png)

![New Kotlin project with the Gradle build system with Groovy DSL](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin-new-project-gradleg.png)

![New Kotlin project with the IntelliJ build system](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin-new-project-idea.png)

![New Kotlin project with the Maven build system](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin-new-project-maven.png)

8. Enable the Use compact project structure option to create a project with a simplified folder structure.

9. Click Create.

## Write code

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.

Procedure:

1. In the Project tool window on the left, expand the node named after your project and open the `/src/main/kotlin/Main.kt` `/src/Main.kt` file.

![src/main/kotlin/main.kt in the Project tool window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/main.kt in the Project tool window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/main.kt in the Project tool window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/main.kt in the Project tool window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_main_kt_maven.png)

2. The file only contains the `main()` function with print statements. This function is the entry point of your program.

Replace the definition of the `main` function with the following code snippet:

```KOTLIN

fun main(args: Array<String>) {
    println("What is your name?")
}

```

3. 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 convert `rl` to `readln()`. Press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (NetBeans), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (Eclipse), `⏎` (Eclipse (macOS)) to accept the suggestion.

![Code completion in action](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_tutorial_cc_1.png)

4. Move the caret to the next line and type `println("Hello, $")`. Press `Ctrl+Space` (Windows), `⌃ Space` (macOS), `⌃ Space` (IntelliJ IDEA Classic (macOS)), `⌃ Space` (macOS System Shortcuts), `Ctrl+Space` (XWin), `Ctrl+Space` (GNOME), `Ctrl+Space` (KDE), `Alt+/` (Emacs), `Ctrl+Space` (Sublime Text), `⌃ Space` (Sublime Text (macOS)), `Ctrl+Space` (NetBeans), `Ctrl+Space` (Visual Studio), `⌃ Space` (Visual Studio (macOS)), `Ctrl+Space` (Eclipse), `⌃ Space` (Eclipse (macOS)) to invoke code completion and select the `name` variable from the list.

![Code completion in action](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_tutorial_cc_2.png)

The final code should look as follows:

```KOTLIN

fun main(args: Array<String>) {
    println("What is your name?")
    val name = readln()
    println("Hello $name!")
}

```

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.

## Run code from IntelliJ IDEA

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.

Procedure:

* Click the Run icon in the gutter and select Run 'MainKt' or press `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Alt+Shift+F6` (NetBeans), `Ctrl+Alt+F5` (Visual Studio), `⌃ ⌥ F5` (Visual Studio (macOS)), `Ctrl+Shift+F10` (Eclipse), `⌃ ⇧ R` (Eclipse (macOS)).

![The Run option in the menu](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_run_1.png)

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](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_run_2.png)

## Package as JAR

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.

> **Note:**
> The instructions are provided for Gradle and Groovy as DSL. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.

> **Note:**
> The instructions are provided for Maven. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.

> **Note:**
> The instructions are provided for IntelliJ build tool. For more information about achieving the same using other build tools, use the Build tool switcher at the top of the page.

Procedure:

1. Open the `build.gradle.kts` `build.gradle` script.

![The Build Gradle script in the project structure](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_tutorial_build_gradle.png)

![The Build Gradle script in the project structure](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_tutorial_build_gradle_groovy.png)

> **Tip:**
> The build script is the file that tells the build tool how exactly to build the project.

> **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.

2. In the build script, add the following task definition:

```KOTLIN
tasks.jar.configure {
    manifest {
        attributes(mapOf("Main-Class" to "org.example.MainKt"))
    }
    configurations["compileClasspath"].forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
```

```GROOVY
tasks.jar {
    manifest {
        attributes 'Main-Class': 'org.example.MainKt'
    }
    from {
        configurations.runtimeClasspath.collect { file ->
            file.isDirectory() ? file : zipTree(file)
        }
    }
}
```

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 the `MainKt` class is in the `org.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.

3. When the definition is added to the build file, press `Ctrl+Shift+O` (Windows), `Ctrl+Shift+O` (macOS), `Ctrl+Shift+O` (IntelliJ IDEA Classic (macOS)), `Ctrl+Shift+O` (macOS System Shortcuts), `Ctrl+Shift+O` (XWin), `Ctrl+Shift+O` (GNOME), `Ctrl+Shift+O` (KDE), `Ctrl+Shift+O` (Emacs), `Ctrl+Shift+O` (Sublime Text), `Ctrl+Shift+O` (Sublime Text (macOS)), `Ctrl+Shift+O` (NetBeans), `Ctrl+Shift+O` (Visual Studio), `Ctrl+Shift+O` (Visual Studio (macOS)), `Ctrl+Shift+O` (Eclipse), `Ctrl+Shift+O` (Eclipse (macOS)) or click ![Reimport All Projects](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.actions.refresh.svg) in the Gradle tool window to import the changes.

4. 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.

![Kotlin Tutorial Gradle Jar](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_tutorial_gradle_jar.png)

The resulting JAR appears in the `build/libs` directory.

![The resulting JAR in the out directory viewed in the Project Tool Window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_tutorial_gradle_jar_2.png)

Procedure:

1. In the main menu, go to `File | Project Structure` `Ctrl+Alt+Shift+S` (Windows), `⌘ ;` (macOS), `⌘ ;` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ Comma` (macOS System Shortcuts), `Ctrl+Alt+Shift+S` (XWin), `Ctrl+Alt+Shift+S` (GNOME), `Ctrl+Alt+Shift+S` (KDE), `Ctrl+Alt+Shift+S` (Emacs), `Ctrl+Alt+Shift+S` (Sublime Text), `⌘ ;` (Sublime Text (macOS)), `Ctrl+Alt+Shift+S` (NetBeans), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS)) and open the Artifacts tab.

2. Click Add ![the Plus icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg), then JAR, then From modules with dependencies.

![Adding new artifact in the Project Structure dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_jar_1.png)

3. In the Main Class field, click the Browse button, and select `Main.kt` as the main class.

![The Select Main Class dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_jar_2.png)

4. Specify the absolute path to `/src/main/resources` as the manifest directory, for example: `/Users/jetbrains/IdeaProjects/greeting/src/main/resources`.

Click OK in the Create JAR from Modules dialog.

![The Create JAR from Modules dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_jar_2_5_maven.png)

![The Create JAR from Modules dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_jar_2_5.png)

5. Click OK in the Project Structure dialog.

6. In the main menu, go to `Build | Build Artifacts`, then click Build.

The resulting JAR appears in the `out` directory.

![The resulting JAR in the out directory viewed using the Project Tool Window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_jar_4.png)

![The resulting JAR in the out directory viewed in the Project Tool Window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/kotlin_create_first_app_maven_4.png)

## Run the JAR

Procedure: Run from IntelliJ IDEA

* 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.

Procedure: Run from CLI

* Open the terminal and from the containing folder, run:

`java -jar greeting.jar` `java -jar greeting.jar` `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`

