So far I have
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
MyProjectCompiler.initialize("SampleKtFileOutput")
.packageName("com.test.sample")
.compile(File(someFile.path))
.result { ktSource: String -> K2JVMCompiler()
.exec(System.out, /** arguments here?*/) }
This manually starts the compiler, but I would like to compile the resulting String from the first compiler (MyProjectCompiler
which generates kotlin source) in-memory and check the result without writing to a file.
I would like to include everything on the current classpath if possible.
I found the easiest way to do it is to use something like the code in the original question and use
java.io.tmpdir
. Here's a re-usable solution:Add the kotlin compiler as a test dependency:
Wrapper for the compiler:
Classloader for creating objects from the compiled classes:
Example test case:
First make a kotlin source file
Make sure it compiles:
Load the class as interface instance
The output will be as expected:
found: 'Hello, world!'