How can kotlinc without gradle or IntelliJ IDEA take less than 10 seconds to compile Hello, World? - kotlin

kotlinc -include-runtime -d hello.jar hello.kt or simply kotlic hello.kt takes over 10 seconds with the below hello word program.
fun main() {
println("Hello, world")
}
I have used oracle's and openjdk's java 17. I am using kotlin 1.80. I have tested it on two Linux machines. I have installed kotlinc using the source and using SDKMAN! I have 4gb of RAM. The same incredibly slow compile time results.
When I compile using javac it is no more than a second for a far more complex project.
Is there any way to speed up the kotlin compile time without using IntelliJ IDEA or gradle?

Related

What magic happens when IntelliJ compiles my Kotlin project?

I currently know that kotlinc hello.kt -include-runtime -d hello.jar compiles my hello world program written in kotlin. Furthermore I am able to execute my program via java -jar hello.jar.
Setting up a Kotlin "Hello world!" console application example in IntelliJ I may choose between different Build Systems (Maven, Gradle, IntelliJ).
I would like to know what happens in the background using the IntelliJ Build System for example?
It seems that there is an ant script running considering the Build Output:
Executing pre-compile tasks…
Loading Ant configuration...
Running Ant tasks...
Running 'before' tasks
Checking sources
Kotlin: connecting to daemon
Kotlin: compiling [firstKotlinApp]
Kotlin: kotlinc-jvm 1.6.10-release-923 (JRE 17.0.1+12-39)
Kotlin: performing incremental compilation analysis
Checking dependencies… [firstKotlinApp]
Dependency analysis found 0 affected files
Updating dependency information… [firstKotlinApp]
Running 'after' tasks
Finished, saving caches…
Executing post-compile tasks…
Loading Ant configuration...
Running Ant tasks...
Synchronizing output directories…
04.01.22, 07:59 - Build completed successfully in 6 sec, 686 ms
Can someone help me out and direct me to the right place to look?
I already tried to find information with several search engines.

After building a Kotlin program in IntelliJ, how do I execute it directly from the terminal? [duplicate]

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.
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
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:
to avoid repetition, i'll be using the short name: $(ideac) = "IntelliJ IDEA Community Edition 2022.2" in place of the full name
the path to the kotlinc is this one: $(ideac)\plugins\Kotlin\kotlinc\bin
much similar to the one told above, but slightly different
i have verified it to work in the command given in the command-line doc linked above or compiler-reference too: kotlinc hello.kt -include-runtime -d hello.jar
running the resultant .jar file via java -jar ./hello.jar shows the expected output
there's no kotlin-native in that folder, so, couldn't verify this native-command-line-compiler kotlin doc
Backstory/What did not work
the command under the run tab in $(ideac) no longer shows the kotlinc path
I had created the Kotlin "project" by using "new project" (intelliJ) or by "Kotlin multiplatform > JVM" (gradle)
The run command for either of these did not contain any kotlinc in them
I tried creating a new > "scratch" file; and it showed this: $(ideac)\plugins\Kotlin\bin\windows\LLDBFrontend.exe but i tried using that on CLI and it did not work either

Compile and run program from Kotlin REPL

I currently compile and run Kotlin programs on the command line using kotlinc, like
konlinc program.kt -include-runtime -d program.jar && jave -jar program.jar
This takes a few seconds to presumably load the compiler into memory each time, give the standard warnings (e.g. All illegal access operations will be denied in a future release) and then starts to compile the program.
As a result, even for a hello world program it takes a couple of seconds.
Is there a way I can do this faster? Compiling and running from the REPL might be a faster way, but I don't know if Kotlin allows that. I don't want to use an IDE.

Running a simple hello world from the console after installing IntelliJ IDEA

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.
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
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:
to avoid repetition, i'll be using the short name: $(ideac) = "IntelliJ IDEA Community Edition 2022.2" in place of the full name
the path to the kotlinc is this one: $(ideac)\plugins\Kotlin\kotlinc\bin
much similar to the one told above, but slightly different
i have verified it to work in the command given in the command-line doc linked above or compiler-reference too: kotlinc hello.kt -include-runtime -d hello.jar
running the resultant .jar file via java -jar ./hello.jar shows the expected output
there's no kotlin-native in that folder, so, couldn't verify this native-command-line-compiler kotlin doc
Backstory/What did not work
the command under the run tab in $(ideac) no longer shows the kotlinc path
I had created the Kotlin "project" by using "new project" (intelliJ) or by "Kotlin multiplatform > JVM" (gradle)
The run command for either of these did not contain any kotlinc in them
I tried creating a new > "scratch" file; and it showed this: $(ideac)\plugins\Kotlin\bin\windows\LLDBFrontend.exe but i tried using that on CLI and it did not work either

SEVERE: class not found error with cmd

I am using Intellij Idea to run a jade program. I have the ".jar" files in the environment variable "classpath". When I run the program from the IDE it works fine and finds the classes defined by me. But when I run the program from CMD it says
class not found.
The java files are in package name "msg". I am giving the following command:
java jade.Boot -gui f1:msg.first
You should specify -classpath attribute for java (and ofc you should compile your java files before).
For example:
java -classpath jade-4.4.0.jar jade.Boot -gui f1:msg.first