Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Setting up the Kotlin environment

Setting up the Kotlin environment

Before writing Kotlin code, you need an environment that can compile and run it.

This article introduces five common approaches, from the easiest online runner to Gradle, Maven, and Android Studio setups for real projects.


Comparison of the five ways to run Kotlin

Different approaches suit different stages, so look at a comparison table before deciding where to start.

Method Installation cost Applicable scenarios Recommended for long-term use
Online runner No installation needed Learning syntax and verifying small snippets No
IntelliJ IDEA Install an IDE and JDK Local learning, debugging, and reading source code Is
Command-line kotlinc Install the compiler Understanding the build process and writing small scripts Depends
Gradle Only a JDK is needed Formal Kotlin/JVM projects Is
Maven Only a JDK is needed Projects already using Maven Is
Android Studio Install an IDE and SDK Android app development Is

Kotlin compile-and-run flowchart: hello.kt is compiled by kotlinc into hello.jar, then run by java -jar to output Hello, World!; Gradle or Maven is an optional alternative build path

Note:Kotlin 2.2 requires JDK 8 or higher. The "JDK 1.6" mentioned in older tutorials is long unsupported and configuring that version will fail outright.


Online runner

If you just want to try a line of code right away, an online tool is the fastest way; you do not even need to install a JDK.

The official Playground URL is play.kotlinlang.org, open it to edit and run code.

Paste the code below into it and click Run to see the output.

Example

// Minimal example for running online

fun main

(

)

{

    val siteName

=

"Runoob Tutorial"

                 

// Declare an immutable variable

    println

(

"Hello, $siteName!"

)

             

// String template

    println

(

"Website: www.runoob.com"

)

}

The output is as follows.



Hello, Runoob Tutorial!

网址:www.runoob.com

Runoob Tutorial'sOnline toolscan also run Kotlin and are convenient for verifying code as you follow a tutorial.

The downside of online tools is that you cannot install dependencies or debug, so they are only good for syntax practice.


IntelliJ IDEA

For local learning, IntelliJ IDEA is the top recommendation: it is JetBrains' own IDE and offers the most complete Kotlin support.

The Community edition is enough to download; it is free for personal study and capable enough for everyday use.

Download address:https://www.jetbrains.com/idea/download/

Usage notes:https://www.runoob.com/kotlin/kotlin-setup.html

After installation, create a new project by following these steps.

Step 1: open IDEA, click New Project, and select Kotlin in the language list on the left.

Step 2: enter a project name, for example kotlin-demo, and make sure Build system is set to IntelliJ.

Step 3: choose a JDK version. JDK 8 or higher works, but a long-term support release such as JDK 17 or JDK 21 is recommended.

Step 4: right-click the src/main/kotlin directory, create a Kotlin file Main.kt, write your code, and click the green triangle next to the line number to run it.

Example

// File path: src/main/kotlin/Main.kt

fun main

(

)

{

    val url

=

"www.runoob.com"

    println

(

"Project created successfully, target site: $url"

)

}

The console prints the following line.



项目创建成功,目标站点:www.runoob.com

IDEA ships with the Kotlin plugin built in, so no separate installation is needed and syntax is recognized automatically when you create a Kotlin file.

Tip:If the new-project wizard cannot find a JDK, specify your local JDK installation directory under File -> Project Structure.


Gradle project

Gradle is currently the most popular build tool for Kotlin/JVM projects; a single configuration file handles both dependencies and compilation tasks.

Kotlin 2.2 uses the Kotlin DSL for build scripts, in a file named build.gradle.kts.

Example

// File path: build.gradle.kts

plugins

{

    kotlin

(

"jvm"

)

version

"2.2.0"

     

// Kotlin/JVM plugin, version must match the compiler

    application                        

// Provides a run task for running the program directly

}

group

=

"com.runoob"

                 

// Project group name, usually a reversed domain name

version

=

"1.0.0"

                     

// Project version

repositories

{

    mavenCentral

(

)

                   

// Download dependencies from Maven Central

}

dependencies

{

    testImplementation

(

kotlin

(

"test"

)

)

// Add the official Kotlin test library

}

application

{

   

// The main class name is "filename + Kt", so Main.kt corresponds to MainKt

    mainClass.

set

(

"com.runoob.MainKt"

)

}

kotlin

{

   

// Specify the JDK version for compilation; Gradle will try to download it automatically if it is not installed locally

    jvmToolchain

(

17

)

}

Add a settings.gradle.kts as well to declare the project name so Gradle can identify the root project correctly.

Example

// File path: settings.gradle.kts

rootProject.

name

=

"kotlin-demo"

     

// Root project name

The recommended standard directory structure is shown below, with all source code under src/main/kotlin.



kotlin-demo/

├── build.gradle.kts

├── settings.gradle.kts

└── src

    └── main

        └── kotlin

            └── com

                └── runoob

                    └── Main.kt

The main class file is shown below; note that the package name must match the directory structure.

Example

// File path: src/main/kotlin/com/runoob/Main.kt

package

com.runoob

                   

// Package declaration, must match the directory structure

fun main

(

)

{

    println

(

"Hello, Runoob!"

)

    println

(

"Tutorial site: www.runoob.com"

)

}

Run the following command in the project root; Gradle will download dependencies, compile, and run automatically.



$ ./gradlew run



> Task :run

Hello, Runoob!

教程站点:www.runoob.com



BUILD SUCCESSFUL in 3s

On Windows, just replace ./gradlew with gradlew.bat.


Maven Project

If your team already uses Maven, you can build Kotlin projects with Maven too; the key is to add kotlin-maven-plugin.

Create a pom.xml in the project root with the following content.

Example

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                            http://maven.apache.org/xsd/maven-4.0.0.xsd">

   

4.0.0

   

com.runoob

             

   

kotlin-demo

     

   

1.0.0

                 

   

       

2.2.0

       

8

 

       

UTF-8

   

   

       

       

           

org.jetbrains.kotlin

           

kotlin-stdlib

           

${kotlin.version}

       

   

   

       

       

src/main/kotlin

       

           

               

org.jetbrains.kotlin

               

kotlin-maven-plugin

               

${kotlin.version}

               

                   

                       

compile

                       

compile

           

                       

                           

compile

                       

                   

               

           

       

   

Put the source in src/main/kotlin/com/runoob/Main.kt and run the command below to compile and run.



$ mvn compile

$ mvn exec:java -Dexec.mainClass=com.runoob.MainKt



Hello, Runoob!

教程站点:www.runoob.com

If exec:java fails with an error, configure the exec-maven-plugin additionally in pom.xml.


Android Studio

Android Studio is based on IntelliJ IDEA, has built-in Kotlin support, and is the standard tool for Android app development.

Choose the Empty Activity template when creating a project; the wizard generates the Kotlin DSL build scripts automatically.

The modern style declares plugins in a plugins { } block; the old Groovy style is outdated.

Example

// File path: app/build.gradle.kts (Android app module)

plugins

{

    id

(

"com.android.application"

)

           

// Android application plugin

    id

(

"org.jetbrains.kotlin.android"

)

     

// Kotlin support for Android

}

android

{

    namespace

=

"com.runoob.demo"

           

// Package namespace

    compileSdk

=

35

                         

// Android SDK version used for compilation

    defaultConfig

{

        applicationId

=

"com.runoob.demo"

   

// Unique application ID

        minSdk

=

21

                         

// Minimum supported Android version

        targetSdk

=

35

                     

// Target Android version

        versionCode

=

1

                     

// Internal version code, must be an integer

        versionName

=

"1.0"

                 

// The version name shown externally

   

}

}

The corresponding Gradle plugin versions are declared uniformly in build.gradle.kts in the project root.

Example

// File path: build.gradle.kts (project root)

plugins

{

    id

(

"com.android.application"

)

version

"8.7.0"

apply

false

         

// Only declare versions here; do not apply them in the root project

    id

(

"org.jetbrains.kotlin.android"

)

version

"2.2.0"

apply

false

   

// Kotlin plugin version

}

Note:In older materials, apply plugin: 'kotlin-android' is the Groovy syntax. It still works today but is no longer recommended; for new projects, use the Kotlin DSL plugins { } block.


Eclipse

JetBrains has long stopped maintaining the Eclipse plugin for Kotlin, so it is not recommended for new projects.

Setting up the Kotlin Eclipse environment:https://www.runoob.com/kotlin/kotlin-eclipse-setup.html

Tip:If you are using Eclipse, you can switch to IntelliJ IDEA Community Edition or Android Studio; both are free and support Kotlin out of the box.


Common questions

The following are the most common issues during environment setup.

Error: Unsupported class file major version

This is caused by a mismatch between the JDK version and the Gradle or Kotlin plugin version.

First confirm that the version reported by java -version is at least 8, then check whether your Gradle version supports that JDK.

Gradle downloads dependencies very slowly

When accessing the Maven Central repository is slow from China, you can add a domestic mirror repository to the repositories block in build.gradle.kts.

IDEA reports that the Kotlin plugin version is too low

Search for Kotlin in the Plugins page of Settings and click Update to upgrade to a version consistent with the compiler.

Garbled Chinese output

Make sure source files use UTF-8 encoding, and set tasks.withType in Gradle { options.encoding = "UTF-8" }。