Reputation: 10012
I have installed IntelliJ IDEA on my mac and wrote the simplest Kotlin program
fun main(args : Array<String>){
println("Hello")
}
I can run it from the IDE environment. (It prints Hello of course)
My question: How can you run this from the console?
What I have done:
I tried to call
java simplekt.class
but I got
Error: could not find or load main class simplekt.class
I tried java simplekt
but then I got an exception in thread main java.lang.NoClassDefFoundError
I tried to use kotlin
or kotlinc
but the command was not found. (where is the compiler installed?)
In this resource they use kotlinc and they produce a jar file but IDEA only output a class file.
Not really sure how to proceed from here.
Upvotes: 3
Views: 812
Reputation: 144
The above answer by @user2340612 is mostly right, but maybe due to software changing or some differences in what i did, it did not work for me. Following did:
$(ideac) = "IntelliJ IDEA Community Edition 2022.2"
in place of the full namekotlinc
is this one: $(ideac)\plugins\Kotlin\kotlinc\bin
kotlinc hello.kt -include-runtime -d hello.jar
java -jar ./hello.jar
shows the expected outputkotlin-native
in that folder, so, couldn't verify this native-command-line-compiler kotlin dockotlinc
in them$(ideac)\plugins\Kotlin\bin\windows\LLDBFrontend.exe
but i tried using that on CLI and it did not work eitherUpvotes: 1
Reputation: 10733
When you run your application from the IDE, in the Run
window the very first line is the command that the IDE executes to start your program. In my case it's something like:
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 <a lot more omitted>
You can execute the same command in your terminal and that will execute the application. Reading that line will also (indirectly) tell you where the kotlinc
command is installed, and in my case – using MacOS – it's at /Applications/IntelliJ\ IDEA.app/Contents/plugins/Kotlin/kotlinc/bin/kotlinc
However, you can always decide to entirely stop using the IDE and compile/run your program from the command line by following instructions here: https://kotlinlang.org/docs/tutorials/command-line.html
Upvotes: 1