Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Kotlin compiler compile java files?

Tags:

java

kotlin

I have a project with both .java and .kt files. Does the Kotlin compiler compiles both .java and .kt files or does it only compile my .kt files?

like image 893
Gana Avatar asked Apr 01 '18 20:04

Gana


People also ask

Does Java compile in Kotlin?

 Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode.

How does Kotlin compile to JVM?

Kotlin/JVM Compiler. Kotlin comes with a compiler that compiles Kotlin source files into Java class files, which we can run on any JVM. Typically, when we build a Kotlin project like a multiplatform library, it automatically uses this compiler to produce class files.

Can Kotlin interact with Java?

Kotlin is designed with Java interoperability in mind. Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used from Java rather smoothly as well.

Does Kotlin compile to machine code?

Yes, when targeting the JVM, Kotlin is compiled to JVM *. class files, which is a bytecode format that can later be either interpreted by a JVM, or compiled to the machine code by the JVM during the program run (JIT), or even compiled ahead-of-time (AOT) down to the machine code.


2 Answers

No, kotlinc compiles only Kotlin files (.kt). A mixed-language project requires combining both kotlinc and javac. If the references flow only one way, e.g., if Java code references Kotlin code and not the other way around, then the compilation order is pretty easy to figure out. In that situation, you would compile the Kotlin code first, then include the output in the classpath to javac when compiling the Java code.

If the two are more intertwined, it becomes a little more complicated. Looking at this thread, it appears you'll need to run kotlinc first, passing in any referenced Java source files (as the .class files won't be available yet). The class files built by kotlinc can then be included in the classpath when you compile your Java files.

like image 125
Mike Strobel Avatar answered Sep 28 '22 06:09

Mike Strobel


By itself it does not, however one of the requirements to run the Kotlin compiler (according to its documentation https://github.com/JetBrains/kotlin) is the JDK, so if you have Kotlin you also have javac

like image 42
Jason Sperske Avatar answered Sep 28 '22 06:09

Jason Sperske