Indie pig.
Indie Pig Indie games and experiences.

Back to "What is Kotlin?"

Monday 24th, November 2014

A Hello World in Kotlin for the JVM

This article is at the time of Kotlin 0.9.206, and some parts are likely to be quickly out of date.

Don't be overwhelmed by the number of steps, most of these are common sense, I'm just trying to be thorough.

This article is to get a Hello World, with unit tests, up and running in Kotlin for the JVM.

Step 1: Install IntelliJ

Download IntelliJ IDEA

This article was written using IntelliJ IDEA 14.

Step 2: Install the Kotlin Plugin

Note: If you are upgrading IDEA and previously had the plugin, be sure to uninstall the Kotlin plugin and get the latest version.

File > Settings > Plugins > Browse Repositories

Step 3: Create a new project

File > New Project

Select Kotlin, Kotlin - JVM

Step 4: Copy library files

You should have an empty project now that looks something like this:

Inside the lib folder you should have a kotlin-runtime.jar and a kotlin-runtime-sources.jar

Step 5: Create a Hello World Kotlin file.

Right click your src directory, New > Kotlin File

Create the file with the name Hello

Step 6: Print "Hello World"

We need a package-level main function. This is our entry point.

fun main(args: Array<String>) {
	println("Hello world")

Pro tip: You can generate this method just by typing "main" then hitting tab.

Step 7: Run the Application

Inside Hello.kt, right click in the editor, and select "Debug '_DefaultPackage'"

We should now see a "Hello World" in the console.

Step 8: Unit tests

Now I am not a Test Driven Development nut. I won't give you my opinions on what are the best frameworks or styles of testing to use, but I do like to write my unit tests alongside my application. I do this mainly because it simply means faster iteration time. I can focus on a single piece of code instead of an entire application.

Before we create a test, we need something simple to test. In your Hello.kt file let's create a simple class.

class Hello {
	fun hello(): String {
		return "hello"
fun main(args: Array<String>) {
	println("Hello world")

Next we need to get a unit testing framework. We can do this easily by adding a maven dependency in our project.

Go to File > Project Structure > Libraries > + New Project Library > From Maven

Then add junit as a dependency.

Next create a folder named "test", and then mark that directory as a test sources root.

Create a new kotlin file in your test folder, choose "class", and call it "HelloTest".

import org.junit.Test;
import kotlin.test.assertEquals
public class HelloTest {
	Test fun helloTest(): Unit {
		val hello = Hello();
		assertEquals("hello", hello.hello())

Pay close attention to the imports. The "Test" annotation is what tells the test runner which methods are tests, and you must be sure the org.junit.Test class is imported for that to work.

We can now run the unit test the same way we did our application. Right click either in a specific test method you want to run, or in the class itself to run all tests in that class, and choose "Debug 'helloTest()'"

If all went smoothly, we should see a passing unit test like this:

I hope this helps people get started with Kotlin!

KotlinJS - Hello World


Add your comment