8

I'm trying to create a Kotlin Vert.x language support module and I need a way to compile Kotlin files and load the results with a ClassLoader. I've tried using kotlin-compiler library and found K2JVMCompiler class, but it seems to support only command-line-style arguments with its exec method. Is there a way to compile a Kotlin file in runtime (possibly without having to save and read .class files) and immediately load the generated classes? (Kind of like Groovy does.) If not, do you have any useful compiler arguments suggestions or pretty much any advices?

Czyzby
  • 2,999
  • 1
  • 22
  • 40
  • This is for Vertx 2? For Vertx 3 you wouldn't do such a thing. – Jayson Minard Jan 18 '16 at 12:37
  • @JaysonMinard, I'm pretty sure this is Vert.x 3: https://github.com/vert-x3/vertx-lang-groovy/blob/master/src/main/groovy/io/vertx/lang/groovy/GroovyVerticleFactory.groovy I know you can use solely .class files and rely on normal Kotlin compilation, but it would be very flexible if you could drop plain .kotlin files and Vert.x would know how to run it. I'm still new to the framework (just found it), so maybe I'll just end up using plain Vert.x libraries (and embedded Vert.x) and see how it goes. – Czyzby Jan 18 '16 at 16:19
  • I think this is an XY Problem (http://xyproblem.info/), so I provided an answer to what I think the answer would be if you had asked X instead of Y. – Jayson Minard Jan 18 '16 at 18:48
  • To take the "easy path of deploying a Kotlin source file verticle" you picked the hardest route possible. – Jayson Minard Jan 18 '16 at 18:48

2 Answers2

2

This feels like an XY Problem. You want to know how to compile Kotlin on the fly so that you can more easily use Vert.x by running from Kotlin source files instead of compiled code. But really the recommended path for Vert.x usage is to create a simple bit of code that deploys your verticle within compiled code.

In the question, your link for language support says Vert.x 2 in the path "vertx.io/vertx2/language_support.html"; which is different than how it is now done in Vert.x 3. I think you are merging two thoughts into one. First that Vert.x 3 wants you to run Java/Kotlin files from source (it doesn't really; that was a Vert.x 2 thing they moved away from for compiled languages), and second that you need custom language support (you don't).

You should try to use Vert.x 3 by running compiled code. To do so, build your classes and run your own main() that deploys a verticle programatically. Your code would be simple as:

import io.vertx.core.Vertx

fun main(args: Array<String>) {
    val vertx = Vertx.vertx()
    vertx.deployVerticle(SomeVerticleOfMine())
}

Alternatively, the docs for running and deploying from the command-line say:

Vert.x will compile the Java source file on the fly before running it. This is really useful for quickly prototyping verticles and great for demos. No need to set-up a Maven or Gradle build first to get going!

And really it is indeed just for prototyping and quick testing, and it isn't any faster than letting your IDE do the same and running from the compiled classes. You also then have debugging features of the IDE which are infinitely valuable.

For a few helper libraries for using Kotlin with Vert.x, view these options:

There is a full sample project of running Vert.x + Kovert (specifically start with the App class). You can look at the code of Kovert to do your own similar work of starting and running Vert.x nicely, with Promises or however you wish. The docs for Kovert have links to code for starting Vertx and also starting a Verticle to use Vert.x-Web, so more sample code you can read. But it helps to understand Injekt (light-weight dependency registry), Kovenant (promises library), and Klutter configuration injection to understand the complete sample.

Other quick note, Vert.x has codegen support for other languages, but since you can call all of the Java version directly, it does not need to support Kotlin either.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • You're right. The documentation is kind of confusing (mixing the old ways with the new ones), but I guess this is a common problem of mature frameworks. I accepted your answer since it solves my particular problem. – Czyzby Jan 19 '16 at 09:45
2

Kotlin 1.1 comes with javax.script (JSR-223) support, which means you can use it as scripting engine similarly to JavaScript with Nashorn.

Czyzby
  • 2,999
  • 1
  • 22
  • 40