Creating Kotlin Project in Eclipse

In this tutorial I will show you how to create Kotlin project in Eclipse. This example shows you how to create Kotlin gradle project or Kotlin maven project.

Kotlin is statically typed programming language for modern multiplatform applications. Kotlin has 100% compatibility with Java or vice-versa.

Salient Features of Kotlin

Drastically reduces the amount of boilerplate code.

Avoids entire classes of errors such as null pointer exception.

Leverages existing libraries for the JVM, Android and the browser.

Choose any Java IDE or build from the command line.


Prerequisites
Eclipse Neon
Gradle or Maven configuration
Java 8 configuration

Please read the following steps in order to create and build Kotlin project in Eclipse.

Step 1. Setup Kotlin plugin in Eclipse. Please follow instructions written here at  https://kotlinlang.org/docs/tutorials/getting-started-eclipse.html or you can install the kotlin plugin by downloading from the Eclipse option Help->Install New Softwares and add site URL  https://dl.bintray.com/jetbrains/kotlin/eclipse-plugin/last/  to download. Accept the License Agreement. When warning appears, accept it. You need to restart the Eclipse in order to reflect the changes.

kotlin project eclipse

Step 2. Now we are done with the above step and you will find Kotlin configurations added to the Eclipse from option File -> New -> Other -> Kotlin. Now from here onward you will be able to create Kotlin projects, files and many more. In this example, I will show you how to create Gradle based Kotlin project. So I will create Gradle project in Eclipse with the below build.gradle file. Let’s say the project name would be “kotlin”.

buildscript {
	ext {
		kotlin_version = '1.2.31'
	}
	repositories {
		mavenLocal()
		mavenCentral()
	}
	dependencies {
		classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
	}
}
apply plugin: "kotlin"
repositories {
	mavenLocal()
	mavenCentral()
}
dependencies {
	compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}")
}

If you need maven based project then you can add following dependencies to the project’s pom.xml file

<properties>
    <kotlin.version>1.2.31</kotlin.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <goals> <goal>compile</goal> </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Step 3. Now we will build the project from command prompt using the command “gradle clean build”. Once required libraries are downloaded you will see “BUILD SUCCESSFUL” message.

Step 4. Now we will create package “com.roytuts.kt” and under this package we will create Kotlin class https://kotlinlang.org/docs/reference/classes.html file Hello.kt

package com.roytuts.kt
class Hello {
	fun sayHello(name: String): String {
		return "Hello " + name;
	}
}
fun main(args: Array<String>) {
	val hello = Hello().sayHello("Soumitra Roy")
	println(hello)
}

Step 5. Run the above class by doing right-click on the Hello.kt file and Run As -> Kotlin Application. You will see below output in the console.

Hello, Soumitra Roy
Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *