29

Jetbrains provides some documentation but I can't find how to run compiled class file of Kotlin.

hello.kt:

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

compile:

$ kotlinc -out dist -src hello.kt
$ ls dist
namespace.class
$ java dist/namespace
Exception in thread "main" java.lang.NoClassDefFoundError: dist/namespace (wrong name: namespace)
$ java -jar /usr/local/kotlin/lib/kotlin-runtime.jar
Failed to load Main-Class manifest attribute from
/usr/local/kotlin/lib/kotlin-runtime.jar 

How to run Kotlin program?

Jk1
  • 11,233
  • 9
  • 54
  • 64
kwatch
  • 508
  • 1
  • 5
  • 12
  • Your selected answer has 0 total resulting votes, and most the answers here are outdated for current Kotlin. Please review the answers and if you cannot change the selected answer, flag for moderator help and explain what you wish to have happen. – Jayson Minard Dec 29 '15 at 04:47
  • Possible duplicate of [How to run Kotlin class from the command line?](http://stackoverflow.com/questions/14733566/how-to-run-kotlin-class-from-the-command-line) – Connor Spencer Harries Jan 05 '16 at 22:23

5 Answers5

53

Knowing the Name of Your Main Class

Currently (Kotlin since M14 including up to 1.0 betas), to run a Kotlin class you are actually running a special class that is created at the file level that hold your main() and other functions that are top-level (outside of a class or interface). So if your code is:

// file App.kt
package com.my.stuff

public fun main(args: Array<String>) {
  ...
}

Then you can execute the program by running the com.my.stuff.AppKt class. This name is derived from your filename with Kt appended (previous versions appended KT but from later betas and 1.0 is Kt). You can change the name of this class within the file by adding this file-targeted annotation:

@file:JvmName("MyApp")  

Or you can also put your main() into a class with a companion object and make it static using the JvmStatic annotation. Therefore your class name is the one you chose:

// file App.kt
package com.my.stuff

public class MyApp {
    companion object {
        @JvmStatic public fun main(args: Array<String>) {
          ...
        }
    }
}

Now for either of these methods, you just run the class com.my.stuff.MyApp

What other JAR files do I need?

You need your application JAR and any dependencies. For Kotlin specific JARs when outside of Maven/Gradle you need a Kotlin distribution which contains:

  • kotlin-runtime.jar (combined runtime and stdlib)
  • kotlin-reflect.jar only if using Kotlin reflection
  • kotlin-test.jar for unit tests that use Kotlin assertion classes

Within Maven/Gradle currently there is also a separate kotlin-stdlib.jar

Running from Intellij

If in Intellij (if it is your IDE) you can right click on the main() function and select Run, it will create a runtime configuration for you and show the fully qualified class name that will be used. You can always use that if you are unsure of the name of the generated class.

Running from Gradle

You can also use the Gradle Application plugin to run a process from Gradle, or to create a runnable system that includes a zip/tgz of your JAR and all of its dependencies, and a startup script. Using the example class above, you would add this to your build.gradle:

apply plugin: 'application'

mainClassName = 'com.my.stuff.AppKt'

// optional:  add one string per argument you want as the default JVM args
applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1g"] 

And then from the command-line use:

// run the program
gradle run

// debug the program
gradle run --debug-jvm

// create a distribution (distTar, distZip, installDist, ...)
gradle distTar

Running Directly from Java Command-line

If you have a runnable JAR, and assuming KOTLIN_LIB points to a directory where Kotlin runtime library files reside:

java -cp $KOTLIN_LIB/kotlin-runtime.jar:MyApp.jar com.my.stuff.AppKt

See the notes above about other JAR files you might need. A slight variation if you have a runnable JAR (with the manifest pointing at com.my.stuff.AppKt as the main class):

java -cp $KOTLIN_LIB/kotlin-runtime.jar -jar MyApp.jar

Running using the Kotlin command-line tool

If you install Kotlin tools via Homebrew or other package manager. (on Mac OS X brew update ; brew install kotlin) Then it is very simple to run:

kotlin -cp MyApp.jar com.my.stuff.AppKt

This command adds the runtime to the classpath provided, then runs the class. You may need to add additional Kotlin libraries as mentioned in the section above "Running from Java."

Creating runnable JAR with the Kotlin compiler

This is not very common since most people use other build tools, but the Kotlin compiler can create a runnable Jar that solves this for you (see http://kotlinlang.org/docs/tutorials/command-line.html) when it bundles the runtime and your code together. Although this isn't as common when using tools such as Maven and Gradle, or IDE builds. Then run using the normal Java:

java -jar MyApp.jar
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • Your answer lacks info about how to run it. – bashor Jan 03 '16 at 11:48
  • `java -cp $KOTLIN_LIB/kotlin-runtime.jar -jar MyApp.jar` will not work: "When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored." from https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html – schemacs Jul 14 '21 at 06:06
6

We ran into the same program and blogged our solution here: http://blog.ocheyedan.net/blog/2012/02/19/running-kotlin-code/

Basically you just need to invoke java with the -cp and the main class of 'namespace'. From your question, the java invocation would look something like this:

java -cp /usr/local/kotlin/lib/kotlin-runtime.jar:dist/namespace.class namespace
Jk1
  • 11,233
  • 9
  • 54
  • 64
blangel
  • 102
  • 2
  • Thankyou, but I can't run my class file by above command. Error "Exception in thread "main" java.lang.NoClassDefFoundError: namespace" happened. – kwatch Feb 20 '12 at 23:42
  • Your classpath to the _java_ process needs to have two things on it; one is the _kotlin-runtime.jar_ where ever that might be on your system as well as all the compiled **Kotlin** code (which from your example looks like that from your _dist_ directory). Just to be sure, have you tried the _Ant_ script from my referenced blog-post? – blangel Feb 24 '12 at 16:51
3

Update: In the newer versions of the Kotlin IDE plugin, you can use context Run-actions in the Editors pop-up menu.

If you are in the IDE, right-click the editor and choose "Run namespace" Otherwise, compile and run the *.namespace class as a normal Java class.

Andrey Breslav
  • 24,795
  • 10
  • 66
  • 61
0

I'm run jar which use kotlin like this

java -cp target/idea_test-1.0-SNAPSHOT.jar:lib/kotlin-runtime.jar testing.first seyfer

Hello seyfer seed!

Oleg Abrazhaev
  • 2,751
  • 2
  • 28
  • 41
0

The docs give a nice and concise answer:

kotlinc hello.kt -include-runtime -d hello.jar

java -jar hello.jar

koclinc is located inside your IntelliJ IDEA directory under IntelliJ\plugins\Kotlin\kotlinc\bin.
If you are running Windows use kotlinc-jvm.bat

Arboreal Shark
  • 2,221
  • 3
  • 17
  • 12