Ben Halton
Ben Halton

Reputation: 103

compile mixed Kotlin and Java code from the command line

I'd like to start adding some Kotlin to my Java project. To do that I need to be able to compile both Java and Kotlin files from the command line, which is fine apart from when files of different types depend on each other e.g. A.java depends on B.kt which in turn depends on C.java.

Is there any way to do this without using Gradle, Maven etc?

Edited to clarify thanks @Nikita for pointing out it is not clear that I want both java and Kotlin files in the same source tree

Upvotes: 9

Views: 2684

Answers (3)

corbin chen
corbin chen

Reputation: 1

Try this https://discuss.kotlinlang.org/t/compiling-mixed-java-and-kotlin-files-on-the-command-line/1553/2

e.g.

kotlinc kotlinSource.kt JavaSource.java -d run.jar

Upvotes: 0

fengma
fengma

Reputation: 181

Remember to compile Kotlin first, then compile Java with kotlin-build-classpath from first step.

simple like this:

1. kotlinc ./src/* -d ./buildTmp
2. javac ./src/**.java -d ./buildTmp -classpath ./buildTmp

Upvotes: 2

Nikita Skvortsov
Nikita Skvortsov

Reputation: 4923

To achieve this, you will need to run two steps.

  1. Run kotlinc targeting *.kt files. Add all required java sources on classpath. Note the destination location.
  2. Run javac targeting *.java files. Add *.class files created by step 1 to classpath.

Result is a combination of *.class files from both steps.

Here is a documetation on Kotlin compiler

Upvotes: 6

Related Questions