# How to run a Kotlin.kt file from command line?

**URL:** <https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836>\
**Category:** Uncategorized\
**Created:** [March 29, 2013, 3:49am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836 "2013-03-29T03:49:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![freewind](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/freewind/32/1479_2.png) [@freewind](https://discuss.kotlinlang.org/u/freewind)\
**Post date:** [March 29, 2013, 3:49am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/1 "2013-03-29T03:49:33Z")

</div>

I created a simple Kotlin file, say "hello.kt":

fun main(args:Array\<String\>) {  
&nbsp;&nbsp;println(“hello, Kotlin!”)  
}

How can I run it from the command line?

---

<div class="post-metadata">

**Author:** ![Natalia.Ukhorskaya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/natalia.ukhorskaya/32/1447_2.png) [@Natalia.Ukhorskaya](https://discuss.kotlinlang.org/u/Natalia.Ukhorskaya)\
**Post date:** [March 29, 2013, 8:39am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/2 "2013-03-29T08:39:48Z")

</div>

You can find an answer here [http://devnet.jetbrains.com/message/5471284](http://devnet.jetbrains.com/message/5471284).

---

<div class="post-metadata">

**Author:** ![chocolateboy](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chocolateboy](https://discuss.kotlinlang.org/u/chocolateboy)\
**Post date:** [May 3, 2013, 5:19am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/3 "2013-05-03T05:19:23Z")

</div>

[https://github.com/andrewoma/kotlin-script](https://github.com/andrewoma/kotlin-script)

---

<div class="post-metadata">

**Author:** ![gautammistry48](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gautammistry48](https://discuss.kotlinlang.org/u/gautammistry48)\
**Post date:** [May 28, 2017, 2:10am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/4 "2017-05-28T02:10:15Z")

</div>

bash\> kotlinc/bin/kotlinc-jvm -src Hello.kt -jar hello.jar  
bash\> java -cp “kotlinc/lib/\*:hello.jar” namespace  
Hello

---

<div class="post-metadata">

**Author:** ![jrfox](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jrfox](https://discuss.kotlinlang.org/u/jrfox)\
**Post date:** [May 31, 2018, 3:46am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/5 "2018-05-31T03:46:25Z")

</div>

Nope - does not work:

I want :

opt\> cat hello.kts

package hello

fun main(args: Array) {  
println(“Hello World!”)  
}

opt\> ./kotlinc/bin/kotlinc hello.kts -d hello.jar  
hello.kts:3:10: warning: parameter ‘args’ is never used  
fun main(args: Array) {  
^  
opt\> java -jar hello.jar  
no main manifest attribute, in hello.jar

to work. Doesn’t the novice Kotlin programmer deserve at least that? Somewhere in the sea of “hello worlds” that DO NOT WORK???

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [May 31, 2018, 6:55am UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/6 "2018-05-31T06:55:29Z")

</div>

Could you please use three backticks (```) before and after code blocks to make code and console sessions more readable? Thanks.

You are compiling a Kotlin script (`kts`) instead of a Kotlin file (`kt`). Change the extension, and you will get a JAR containing class `hello.HelloKt` with a static `main`, and a manifest marking this class as the main class.

This does not mean that it will work using `java -jar hello.jar`. It does not work because the Kotlin run-time is not on the class path.

The easiest way is to use the `kotlin` command. This will add the Kotlin run-time to the class path:

```auto
kotlin -cp hello.jar hello.HelloKt

```

Note that `kotlin hello.jar` won’t work, because then the run-time is not added to the class path. I have no idea why it is not added in this case though.

If you want to run a Kotlin script directly, you have to put the body of `main(...)` at the top level, and use `kotlinc`. See [Executing Kotlin scripts in command shell - #2 by udalov](https://discuss.kotlinlang.org/t/executing-kotlin-scripts-in-command-shell/1926/2)

---

<div class="post-metadata">

**Author:** ![arunabhdas](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arunabhdas/32/9121_2.png) [@arunabhdas](https://discuss.kotlinlang.org/u/arunabhdas)\
**Post date:** [January 21, 2022, 7:36pm UTC](https://discuss.kotlinlang.org/t/how-to-run-a-kotlin-kt-file-from-command-line/836/7 "2022-01-21T19:36:13Z")

</div>

Install kotlin native using homebrew from

> **[kotlin-native](https://formulae.brew.sh/cask/kotlin-native)**
>
> Homebrew’s package index

as follows

brew install --cask kotlin-native

Then run

kotlinc-native hello.kt -o hello
