Compiling Mixed Java and Kotlin Files on the Command Line


#1

It’s all in the title, how to do this

  • without using a build tool (ant/maven/gradle/what not)
  • without painstakingly avoiding cyclic dependencies between Java and Kotlin

From what I’ve seen so far, it seems impossible, but maybe (hopefully) I’m missing something.

If it is impossible, consider fixing it? If plugins for build tools and IDEA can do it, there’s not reason not to have this feature on the command line as well.


#2

You should first invoke kotlinc to compile Kotlin files, then javac to compile Java, passing the result of the first compilation in the classpath. If Kotlin sources reference something from Java, the compiler will load the corresponding Java sources automatically, provided they are located in one of the source directories passed to the command line compiler. Currently it’s not possible to provide paths to individual .java files to be loaded by the Kotlin compiler, or to provide a directory with an intention of only loading .java files from there: each source directory you pass to kotlinc will result in .kt files being compiled and .java files being loaded if needed.


#3

Is this still correct? Or did something change?


#4

This is still correct. The only thing that changed is:

Currently it’s not possible to provide paths to individual .java files to be loaded by the Kotlin compiler

It’s now possible to pass individual files to the Kotlin compiler, e.g. kotlinc kotlinSource.kt JavaSource.java


#5

Ok, but this doesn’t mean that the kotlin compiler will actually compile that java class for you right, it will only not complain that it’s a provided source.
The step of calling javac with all java files and kotlinc generated classes is still “mandatory”, right?