Compiling Mixed Java and Kotlin Files on the Command Line

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.

1 Like

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.


Is this still correct? Or did something change?

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

1 Like

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?

I want to run in powershell the Kotlin-file witch is main.kt witch has a jar-files as a dependencies for the main.kt file in the same folder and it has kotlin-files dependencies at the pre-folder

I tried to run the command “kotlin .\main.kt” and it writes error: unresolved reference: rmqcommunication… import dlm.listeners.Initiator … and more imports and when I try to add the command “kotlin main.kt -classpath .(*.jar) -d dist” its write an error:"source entry it is not a kotlin file: …

So you are saying that the only way is to convert the jar files to a class files and the to convert the kotlin file to a class file and then run it?