9

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

2
  • 1
    why would you not use a build tool? Commented Nov 21, 2015 at 20:39
  • I am also interested coz I want to compile mixed java/kotlin source files in memory. @Ben Haiton do you have any update on this? Commented Mar 3, 2017 at 0:55

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm I couldn't get your solution to work. If I run something like: kotlinc src/**/*.kt -classpath src/**/*.java -d dist then I get an error like: error: source entry is not a Kotlin file: src/java/SomethingElse.java. What does seem to work is kotlinc src/**/ -d dist followed by javac
Sorry. I did not think that you have a mix of kotlin and java files inside one source tree. Thank you for the followup
2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.