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?
Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With