# Mixing Kotlin and Java in a JPMS Module Gradle Project

**URL:** <https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011>\
**Category:** Help/Discuss\
**Created:** [March 20, 2024, 7:39pm UTC](https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011 "2024-03-20T19:39:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![StaticNoiseLog](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@StaticNoiseLog](https://discuss.gradle.org/u/StaticNoiseLog)\
**Post date:** [March 20, 2024, 7:39pm UTC](https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011/1 "2024-03-20T19:39:07Z")

</div>

The goal is to write libraries that make use of the JPMS module system and implementation is with a mix of Kotlin and/or Java.

I am using Gradle 8.6 and JDK 21. The example project was created with “gradlew init” as a library.

The attached example project works (start “Runner.java”, which calls Kotlin code).  
But JPMS seems to get in the way. As soon as module-info.java is added (rename module-info.XXXjava to module-info.java), the Java code cannot access the Kotlin code anymore.

```gradle
> Task :lib:compileJava FAILED
C:\Users\taakabe4\IdeaProjects\kotlinjava\lib\src\main\java\org\example\Runner.java:6: error: cannot find symbol
        var lib = new Library();

```

Is there a (reasonable) way to solve this or is it simply not supported?  
[kotlinjava.zip](https://discuss.gradle.org/uploads/short-url/p2GVMwjCM4j5R1ZgwULqe8qqdRP.zip) (104.6 KB)

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [March 20, 2024, 7:59pm UTC](https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011/2 "2024-03-20T19:59:50Z")

</div>

```kotlin
tasks.compileJava {
    options.compilerArgumentProviders.add(object : CommandLineArgumentProvider {
        @CompileClasspath
        val kotlinClasses = kotlin.sourceSets.main.flatMap { it.kotlin.classesDirectory }

        override fun asArguments() = listOf(
            "--patch-module",
            "kotlinjava.lib.main=${kotlinClasses.get().asFile.absolutePath}"
        )
    })
}

```

---

<div class="post-metadata">

**Author:** ![StaticNoiseLog](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@StaticNoiseLog](https://discuss.gradle.org/u/StaticNoiseLog)\
**Post date:** [March 21, 2024, 7:59pm UTC](https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011/3 "2024-03-21T19:59:35Z")

</div>

Thanks a lot! Works perfectly for the provided example project.

Unfortunately with the “real world project” I get the error below.  
While the real project is still Gradle 8.6, I am forced to use JDK 11 and old Kotlin 1.6.21.  
The project structure is the same (generated with "gradlew init” as a library), but it has a buildSrc and uses various plugins, for example maven-publish and extra-java-module-info. The error might imply that `it.kotlin.classesDirectory` is empty, but I am not sure about this and cannot see a reason why. The o

Any suggestions in what direction to look for a solution?

```gradle
* What went wrong:
A problem was found with the configuration of task ':lib:compileJava' (type 'JavaCompile').
  - Type 'org.gradle.api.tasks.compile.JavaCompile' property 'options.compilerArgumentProviders.$0.kotlinClasses' doesn't have a configured value.
    
    Reason: This property isn't marked as optional and no value has been configured.
    
    Possible solutions:
      1. The value of 'options.compilerArgumentProviders.$0.kotlinClasses' is calculated, make sure a valid value can be calculated.
      2. Mark property 'options.compilerArgumentProviders.$0.kotlinClasses' as optional.

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [March 21, 2024, 8:42pm UTC](https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011/4 "2024-03-21T20:42:39Z")

</div>

Yeah, that’s a KGP bug:

```kts
tasks.compileJava {
    options.compilerArgumentProviders.add(object : CommandLineArgumentProvider {
        @CompileClasspath
        // work-around for Kotlin Gradle Plugin <1.8.20 not setting the classDirectory properly
        //val kotlinClasses = kotlin.sourceSets.main.flatMap { it.kotlin.classesDirectory }
        val kotlinClasses = tasks.compileKotlin.flatMap { it.destinationDirectory }

        override fun asArguments() = listOf(
            "--patch-module",
            "kotlinjava.lib.main=${kotlinClasses.get().asFile.absolutePath}"
        )
    })
}

```

---

<div class="post-metadata">

**Author:** ![StaticNoiseLog](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@StaticNoiseLog](https://discuss.gradle.org/u/StaticNoiseLog)\
**Post date:** [March 22, 2024, 7:33pm UTC](https://discuss.gradle.org/t/mixing-kotlin-and-java-in-a-jpms-module-gradle-project/48011/5 "2024-03-22T19:33:01Z")

</div>

Thank you so much!!!

I was about to give up on my plans to establish a clean modular structure for our future library projects. A dull and mediocre life was starting to unfold in front of me. 😩

But your second version also solved it for the legacy Kotlin version I am stuck with! 👍

May God bless you and your knowledge that easily leaves GPT-4 in the dust. 😄
