Use Kotlin compiler from Java without files?


Since Kotlin appears to be a well-designed language with a number of very good design decisions and good interop with Java, I am thinking about including it as a language to use for programming solutions in a Java-based framework I am working on. So far this has been done by using embedded Java and Scala compilers.

What would be needed is a clean API that allows to directly compile Kotlin code present in memory, into memory, and make the compiled classes available directly through a classloader. The requirement is that the compilation must be performed memory to memory and ideally, all dependencies get resolved from a classloader, not by looking at actual jar files (this is where embedded Scala compiling can cause trouble).

Is this possible with Kotlin? If yes, could you please point me to some documentation or instructions how?
To be honest, the idea is to only look into Kotlin more in detail once it has been established that this is possible, if not, it does not make sense to consider it for this project.


Hmm. it should be “possible”. There is an embedded Kotlin compiler, but with unstable API and not documented or really supported. I am not 100% sure how it loads classes, but classloaders are much easier. The harder bit is writing classes. A single source file can be compiled into many classes (even if not triggering the compilation of other source files that are depended on (but possibly out of date or not compiled yet). Have a look at the source, the compiler is open source so you can see how it works. What you will not get is a clean API.

But is the problem of files really that big? You can always use a temporary ramdisk (tmpfs on Linux) that provides the file api without hitting disk (it is in memory).


A Kotlin support for JSR-223 can match your use case, unfortunately I don’t think this tip can help to solve your problem.


So you are saying there is JSR-223 support for Kotlin?
This may at least allow me to solve (a large?) part of what I would need, but only if the code that executes in the kotlin JSR-223 engine can access everything provided by my own classloader. It would be even better if what gets compiled from Kotlin also can get handled by my classloader, but as long as the Kotlin-compiled code can be made to load from my classloader, many good things should work.
Is there some example code somewhere for how to do this?


The requirement for compiling into ram and making it accessible to a classloader is not based on size, but on the way how kotlin should get embedded and how the whole software gets deployed.


No, unfortunately


Ah ok, I guess this is not really an option then. Pity, having a well designed compiler API and JSR-223 support would sure allow a lot of new use-cases for the language.

I am still not sure if I should give up totally though – the integration with the intellij ide has got to use something similar to what i need. Sadly, that code is obviously not open source so I could have a look at it.


Kotlin does have JSR-223 support since version 1.1. See


Nice to know that,
is it possible to document the integration on
Now it looks tricky to find.


See this post where I experimented with JSR-223 support and figured out the necessary steps to make it work: Embedding Kotlin as scripting language in Java apps


I think this is what you’re looking for (Kotlin plugin for IDEA):

Also IDEA Community is open source: