How to Compile Kotlin Code at Runtime from Java?

Hey,
I have a task where I need to compile Kotlin source code at runtime from within a Java application. Is kotlin-compiler-embeddable the recommended way to achieve this?

For now, I am using K2JVMCompiler, but I’m not sure if this is the best and most stable way. Is this the right tool for compiling Kotlin code from Java at runtime, or are there better alternatives or best practices I should consider?

1 Like

Depending on the nature of your task one of possible solutions would be to compile that kotlin code into javascript and then ‘plug’ that compiled javascript into jvm via javascript engine ( Nashorn etc.)

Hi Semenov,

I am trying to do the same thing, but I have been getting an error on compilation ( Error when compiling with K2JVMCompiler dynamically in Java )

Did you have this problem?