Hello, world with Kotlin and Maven

Kotlin seems like an interesting language for Java enterprise developers who want to introduce a new simpler language with modern functional features but who can’t realistically leave their legacy Java code behind. Also, Kotlin is super easy to read, learn, and code — IMO, anyway.

I noticed a lot examples are based on Gradle. I think Gradle is great. But since most of the Java developers I work with are used to Maven and the project object model (“POM”), I thought this post might be somewhat useful for you. First, we need is a simple Maven POM:

Next, create a file named hello.kt in src/main/kotlin. Create a new source file named hello.kt (the name is important) and add this code:

You can see right away that Kotlin is different: the function is named differently, the arguments are defined differently, and there are no class references anywhere — even the function lives outside a class!

How to Run It

So that’s easy enough to get your Maven project set up for development. But what do you do with it? And how do you actually run it in the real world?

Inside the IDE

If you’re running in JetBrains’s IntelliJ, running it is as simple as clicking the Kotlin icon (among other methods):

Easy but obivously only useful if you’re code will never leave the IDE.

Assemble an Executable Kotlin Jar with Maven

To build a JAR that you can execute in a runtime environment, we’ll need to do some more work on your pom.xml file. In the build section, you need to add two additional plugins. First is the jar plugin:

This is a standard Maven plugin for adding a file, which will tell the Java runtime which class to execute.

You might notice the mainClass element contains the cryptic looking word “HelloKt.” When you go a little further with Kotlin, you’ll learn that it’s completely interoperable with Java. When you want to call Kotlin code from Java, the Kotlin compiler names your classes with Kt suffix appended to the Kotlin source file name.

In our case we named our source file hello.kt, so Kotlin will expose our code to the Java world as HelloKt. You can override the naming with annotations, but I’ll save that for another post.

Now you can assemble an executable jar file. To run the file, however, you’ll need to put additional runtime Kotlin libraries on the classpath. So let’s build a fat jar with the runtime dependencies baked in by adding one more standard Maven plugin in our Maven build process:

Now drop into your command line, run mvn package on your project and you should be able to execute this command:

And Hello, world!

For More Information

JetBrains has a nice, succinct Maven guide for Kotlin with current version numbers. Also, this code is available on my Github account here:


Thanks to a comment from Eric for causing me to update the example with some guidance on how to actually run it!

3 replies on “Hello, world with Kotlin and Maven”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.