[ACCEPTED]-how to run compiled class file in Kotlin?-kotlin

Accepted answer
Score: 52

Knowing the Name of Your Main Class

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

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

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

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

@file:JvmName("MyApp")  

Or you can also put your main() into a class 41 with a companion object and make it static using 40 the JvmStatic annotation. Therefore your class name 39 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 38 methods, you just run the class com.my.stuff.MyApp

What other JAR files do I need?

You need 37 your application JAR and any dependencies. For 36 Kotlin specific JARs when outside of Maven/Gradle 35 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 34 Maven/Gradle currently there is also a separate 33 kotlin-stdlib.jar

Running from Intellij

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

Running from Gradle

You can also use the 26 Gradle Application plugin to run a process from Gradle, or to create 25 a runnable system that includes a zip/tgz 24 of your JAR and all of its dependencies, and 23 a startup script. Using the example class 22 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 21 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 20 runnable JAR, and assuming KOTLIN_LIB points to a 19 directory where Kotlin runtime library files 18 reside:

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

See the notes above about other JAR 17 files you might need. A slight variation 16 if you have a runnable JAR (with the manifest 15 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 14 Kotlin tools via Homebrew or other package manager. (on 13 Mac OS X brew update ; brew install kotlin) Then it is very simple to run:

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

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

Creating runnable JAR with the Kotlin compiler

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

java -jar MyApp.jar
Score: 6

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

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

java -cp /usr/local/kotlin/lib/kotlin-runtime.jar:dist/namespace.class namespace
Score: 3

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

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

Score: 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 1 seyfer seed!

Score: 0

The docs give a nice and concise answer:

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

java -jar hello.jar

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

More Related questions