How to call Kotlin compiler to compile some Kotlin source files from Java?


I want to call Kotlin compiler to comiple some Kotlin source files from Java, how to do it?

I mean to invoke some classes and functions, not calling command line from Java.


Have a look at the sources of build tools that run Kotlin:

For example, here’s what Maven does:


It seems Kotlin compiler can only accept source dirs and output the generated bytecode to some dirs. Is it possible to pass some kotlin files and get the generate bytecode directly?


Yes, you can add individual source files instead of dirs. This does not apply to outputs, because we generate more class files than there're input source files (you can declare many classes in a source file).


Can we get generated bytecode back directly, instead of .class files?


Yes, have a look at how class files are written


Thank you!