# Kotlin Support for Java 9 Module System?

**URL:** <https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499>\
**Category:** Uncategorized\
**Created:** [April 15, 2017, 9:32am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499 "2017-04-15T09:32:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas.Kratz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/thomas.kratz/32/5345_2.png) [@Thomas.Kratz](https://discuss.kotlinlang.org/u/Thomas.Kratz)\
**Post date:** [April 15, 2017, 9:32am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/1 "2017-04-15T09:32:53Z")

</div>

I wonder if Kotlin will support/integrate with the new module system? Is this on your roadmap?  
Will the Compiler be aware of a module? Can I create a module in Kotlin?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [April 18, 2017, 11:30am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/2 "2017-04-18T11:30:31Z")

</div>

Yes, supporting the Java 9 module system is one of the main priorities for Kotlin 1.2. The compiler will be fully aware of the new visibility rules. You’ll be able to create a module in Kotlin; the module-info file will have to remain in Java (because it’s effectively a separate DSL, and we don’t see much value in reinventing it), but the rest of implementation can be Kotlin.

---

<div class="post-metadata">

**Author:** ![guenhter](https://avatars.discourse-cdn.com/v4/letter/g/838e76/32.png) [@guenhter](https://discuss.kotlinlang.org/u/guenhter)\
**Post date:** [December 18, 2017, 3:26pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/3 "2017-12-18T15:26:17Z")

</div>

Kotlin 1.2 is out, so is this now possible? When I change the JDK version in my gradle file to 9, I get this:

e: Unknown JVM target version: 9.0  
Supported versions: 1.6, 1.8

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [December 18, 2017, 4:47pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/4 "2017-12-18T16:47:48Z")

</div>

Please make sure you’re using a recent Gradle version.

---

<div class="post-metadata">

**Author:** ![guenhter](https://avatars.discourse-cdn.com/v4/letter/g/838e76/32.png) [@guenhter](https://discuss.kotlinlang.org/u/guenhter)\
**Post date:** [December 18, 2017, 5:04pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/5 "2017-12-18T17:04:07Z")

</div>

```
./gradlew --version

------------------------------------------------------------
Gradle 4.4
------------------------------------------------------------

Build time: 2017-12-06 09:05:06 UTC
Revision: cf7821a6f79f8e2a598df21780e3ff7ce8db2b82

Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_152 (Oracle Corporation 25.152-b16)
OS: Linux 4.9.0-3-amd64 amd64

```

This is the m most recent of gradle I guess.

---

<div class="post-metadata">

**Author:** ![pltfij](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pltfij/32/4690_2.png) [@pltfij](https://discuss.kotlinlang.org/u/pltfij)\
**Post date:** [January 24, 2018, 7:54am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/6 "2018-01-24T07:54:50Z")

</div>

The same problem.  
From [https://kotlinlang.org/docs/reference/using-gradle.html](https://kotlinlang.org/docs/reference/using-gradle.html) :

> Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6  
> Possible values: “1.6”, “1.8”

So where is java 9?

---

<div class="post-metadata">

**Author:** ![torbilicious](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/torbilicious/32/4720_2.png) [@torbilicious](https://discuss.kotlinlang.org/u/torbilicious)\
**Post date:** [January 31, 2018, 12:47pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/7 "2018-01-31T12:47:33Z")

</div>

push

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [January 31, 2018, 3:33pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/8 "2018-01-31T15:33:33Z")

</div>

Kotlin currently doesn’t support JVM 9 bytecode generation, however JVM 1.8 bytecode is fully compatible with Java 9.

If you want to build/consume modular jars, you need to author `module-info.java` file in your project. This isn’t different from a java-only project, you can refer to this [Gradle guide](https://guides.gradle.org/building-java-9-modules/) for details on how to do that.

---

<div class="post-metadata">

**Author:** ![robstoll](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/robstoll/32/4488_2.png) [@robstoll](https://discuss.kotlinlang.org/u/robstoll)\
**Post date:** [March 11, 2018, 12:15pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/9 "2018-03-11T12:15:26Z")

</div>

I followed the mentioned guide but get stuck at the point that the build fails with errors like the following:

```auto
module-info.java:2: error: package is empty or does not exist: ch.tutteli.atrium.assertions

```

I am able to tweak the build with the [solution mentioned in a SO question](https://stackoverflow.com/questions/47657755/building-a-kotlin-java-9-project-with-gradle):

```auto
compileKotlin.destinationDir = compileJava.destinationDir

```

but it seems like a hack to me, is there kind of an official way of doing it?

---

<div class="post-metadata">

**Author:** ![robstoll](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/robstoll/32/4488_2.png) [@robstoll](https://discuss.kotlinlang.org/u/robstoll)\
**Post date:** [March 14, 2018, 9:25pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/10 "2018-03-14T21:25:41Z")

</div>

I currently use the following gradle code in my root build.gradle, where I can place the module-info.java under src/module

```auto
if (JavaVersion.current() >= JavaVersion.VERSION_1_9) {
    subprojects {
        def moduleInfo = file("${project.projectDir}/src/module/module-info.java")
        if (moduleInfo.exists()) {

            sourceSets {
                module {
                    java {
                        srcDirs = ['src/module']
                        compileClasspath = main.compileClasspath
                        sourceCompatibility = '9'
                        targetCompatibility = '9'
                    }
                }
                main {
                    kotlin { srcDirs += ['src/module'] }
                }
            }

            compileModuleJava.configure {
                dependsOn compileKotlin
                destinationDir = compileKotlin.destinationDir
                doFirst {
                    options.compilerArgs = ['--module-path', classpath.asPath,]
                    classpath = files()
                }
            }
            jar.dependsOn compileModuleJava
        }
    }
}

```

_Edit_: I added `src/module` to the source of main.kotlin in order that the kotlin compiler analyses the module-info.java and complains if necessary.

@ilya.gorbunov when is kotlin turning its jars into modules? I currently get the following warning because I do not want to specify kotlin.stlib for every module:

```auto
warning: requires transitive directive for an automatic module
    requires transitive kotlin.stdlib;

```

---

<div class="post-metadata">

**Author:** ![fabriciorissetto](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fabriciorissetto/32/5123_2.png) [@fabriciorissetto](https://discuss.kotlinlang.org/u/fabriciorissetto)\
**Post date:** [June 29, 2018, 4:55pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/11 "2018-06-29T16:55:21Z")

</div>

Not sure if I got it right @ilya.gorbunov. You mean… is it possible to compile my Kotlin source code (with JPMS modules) targeting JDK 8?

I am currently trying to build a simple HelloWorld project (without gradle) and it raises error when compiling targeting JDK8:  
`Error:Kotlin: Module java.base cannot be found in the module graph`

When targeting higher JDK i.e. 10, it compiles successfull.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [June 29, 2018, 11:45pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/12 "2018-06-29T23:45:16Z")

</div>

I didn’t say and didn’t mean you can target JDK 8 to build modular code, I meant you can target JDK 9 or 10 and compile to the JVM 8 bytecode which can be executed on JVM 9 and 10.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [June 29, 2018, 11:50pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/13 "2018-06-29T23:50:43Z")

</div>

> [@robstoll](#):
>
> when is kotlin turning its jars into modules?

We have to wait until Android build tools of version 3.2 get large adoption, because the previous versions don’t work well with modular jars, see [https://youtrack.jetbrains.com/issue/KT-21266](https://youtrack.jetbrains.com/issue/KT-21266) and [https://issuetracker.google.com/issues/77587908](https://issuetracker.google.com/issues/77587908) for details.

---

<div class="post-metadata">

**Author:** ![rgoldberg](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@rgoldberg](https://discuss.kotlinlang.org/u/rgoldberg)\
**Post date:** [July 16, 2018, 7:37am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/14 "2018-07-16T07:37:13Z")

</div>

Once Kotlin & Gradle together properly support JPMS modules, for a build that only has Kotlin source, plus a `module-info.java` but no other Java source, will the output directories for `compileJava` & `compileKotlin` have to be the same, as per the [aforementioned SO hack](https://stackoverflow.com/a/47669720/2030088)? Or will the output directories be able to be separate on a per-language basis, as is the current case for non-JPMS Gradle outputs?

If the latter, how will this be supported? Is there any way for `javac` to include already compiled `class` files in a module defined in a `module-info.java` in the current build?

---

<div class="post-metadata">

**Author:** ![napperley](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@napperley](https://discuss.kotlinlang.org/u/napperley)\
**Post date:** [July 17, 2018, 2:43am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/15 "2018-07-17T02:43:17Z")

</div>

> We have to wait until Android build tools of version 3.2 get large adoption, because the previous versions don’t work well with modular jars, see [https://youtrack.jetbrains.com/issue/KT-21266](https://youtrack.jetbrains.com/issue/KT-21266) 12 and [https://issuetracker.google.com/issues/77587908](https://issuetracker.google.com/issues/77587908) 7 for details.

@ilya.gorbunov - Are you saying that Kotliner’s using Kotlin on the JVM (eg for server-side development) instead of Android have to **wait** for the above issue to be resolved, even though the issue is specific to Android and not the JVM itself?

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [July 19, 2018, 10:56pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/16 "2018-07-19T22:56:44Z")

</div>

Yes, unfortunately it is so. We don’t want to introduce a change that will break the build for the significant part of our users.

As a temporary alternative we’re considering to build and publish modular artifacts of our libraries under separate maven classifier, say `org.jetbrains.kotlin:kotlin-stdlib:1.2.nn:modular`. Would that help, in your opinion?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 20, 2018, 5:19am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/17 "2018-07-20T05:19:18Z")

</div>

I I think it is a great idea. I still have to wait for tornadofx to move to Java 9, but I hope they will do it soon.

---

<div class="post-metadata">

**Author:** ![napperley](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@napperley](https://discuss.kotlinlang.org/u/napperley)\
**Post date:** [July 20, 2018, 6:01am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/18 "2018-07-20T06:01:23Z")

</div>

> As a temporary alternative we’re considering to build and publish modular artifacts of our libraries under separate maven classifier, say org.jetbrains.kotlin:kotlin-stdlib:1.2.nn:modular. Would that help, in your opinion?

It would work for most situations (about 75% as a **rough** guesstimate), however the pragmatic move wouldn’t be enough in situations where Kotliner’s need to easily deploy a program via the [jlink](https://steveperkins.com/using-java-9-modularization-to-ship-zero-dependency-native-apps/) tool. One of the key requirements mandated by jlink is that the entire program is modularized (incl all dependencies).

---

<div class="post-metadata">

**Author:** ![reitzig](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/reitzig/32/6970_2.png) [@reitzig](https://discuss.kotlinlang.org/u/reitzig)\
**Post date:** [October 11, 2018, 8:22am UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/19 "2018-10-11T08:22:29Z")

</div>

> [@ilya.gorbunov](#):
>
> Yes, unfortunately it is so. We don’t want to introduce a change that will break the build for the significant part of our users.

Meta comment: since the lag between “feature hits JVM/JDK” and “features hits Android” seems to grow with time, and “feature is available on most Android devices” is even worse, I have to wonder whether that strategy is robust.

I mean, Android doesn’t even support Java 8 fully. (At least syntax level, which isn’t as relevant here.)

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [October 11, 2018, 6:18pm UTC](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499/20 "2018-10-11T18:18:19Z")

</div>

> [@ilya.gorbunov](#):
>
> Yes, unfortunately it is so. We don’t want to introduce a change that will break the build for the significant part of our users.

> [@reitzig](#):
>
> Meta comment: since the lag between “feature hits JVM/JDK” and “features hits Android” seems to grow with time, and “feature is available on most Android devices” is even worse, I have to wonder whether that strategy is robust.

The problem is not in Android installed on Android devices, but rather in the Android build tools. And by users I mean the developers who use these build tools.

[Next page](https://discuss.kotlinlang.org/t/kotlin-support-for-java-9-module-system/2499.md?page=2)
