Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll present how to compile a Kotlin class. Afterward, we’ll show how to run the class from the command line. Finally, we’ll demonstrate how to manipulate the main class name.

2. Compiling a Class

To demonstrate the compilation and execution of a class, let’s first define a simple class:

package com.baeldung.run

class RunClass {
    fun printInsideClass() {
        println("Running inside the RunClass")
    }
}

fun main(args: Array<String>) {
    println("Running the main function")
    RunClass().printInsideClass()
}

It consists of the main function, which is an entry point when running the RunClass. The main function is mandatory for running the class. The args argument is optional. Additionally, it contains a function printInsideClass, to show that it will be fired as well.

Let’s now compile it. For compilation, we’ll use the kotlinc command. First, we can compile it with dependencies:

$ kotlinc RunClass.kt -include-runtime -d example.jar

It produces a runnable JAR, example.jar, which contains Kotlin’s runtime dependencies.

Additionally, we can run the same command without the -include-runtime option. It’ll produce a library JAR without runtime. If we would like to run it, we must add runtime to the classpath.

Finally, we can just compile the class itself:

$ kotlinc RunClass.kt

It produces two classes, RunClass.class and RunClassKt.class. 

3. Executing a Class From the Command Line

Let’s now execute the class. In case the JAR contains Kotlin’s runtime, we can simply run it with:

$ java -jar example.jar
Running the main function
Running inside the RunClass

As we can see, it prints out both statements to the console.

Let’s now run the JAR that doesn’t contain Kotlin’s dependencies in itself:

$ java -cp "kotlin-stdlib.jar;example.jar" com.baeldung.run.RunClassKt
Running the main function
Running inside the RunClass

First of all, the kotlin-stdlib.jar is in the classpath. It provides all of Kotlin’s dependencies. For the purpose of this exercise, we put the runtime in the same directory as our example.jar. By default, it’s provided in the lib directory within the Kotlin compiler.

Additionally, the example.jar contains our class. The last provided parameter is our class name.

Now, let’s run the class, which is not archived into a JAR. The compilation above produced two classes. The one ending with ‘Kt’ contains the main function.

Let’s execute the RunClassKt class:

$ java -cp ".;kotlin-stdlib.jar" com.baeldung.run.RunClassKt
Running the main function
Running inside the RunClass

We added to the classpath the directory where we compiled the RunClass. Additionally, same as in the previous case, we added Kotlin’s runtime. Finally, there is the class name as the last parameter.

4. Changing the Name of the Main Class

Let’s now have a look at how we can manipulate the name of a runnable class. First, we can put the main function inside the companion object:

class RunClass {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Running main function")
        }
    }
}

After compilation with kotlinc RunClass.kt, we can run the class using the RunClass name:

$ java -cp ".;kotlin-stdlib.jar" com.baeldung.run.RunClass
Running main function

After that, let’s use the JvmName annotation to set the class name. The value indicates the class name, which will be generated in bytecode:

@file:JvmName("CustomName")
package com.baeldung.run

class RunClass {
    fun printInsideClass() {
        println("Running inside the RunClass")
    }
}

fun main(args: Array<String>) {
    println("Running the main function")
    RunClass().printInsideClass()
}

Let’s compile the class with kotlinc RunClass.kt and execute it:

$ java -cp ".;kotlin-stdlib.jar" com.baeldung.run.CustomName
Running the main function
Running inside the RunClass

That is to say, we were able to run the CustomName class.

5. Conclusion

In this short article, we showed how to compile and run a Kotlin class from the command line. Additionally, we presented how to change the class name.

As always, the source code of the example is available over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!