Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin + Java 9 modules: Module java.base cannot be found in the module graph

I'm having some issues on building a Kotlin project that uses Java 9 features. I know kotlin just allow Java 8 bytecode generation, but acording to kotlinlang.org it should support this functionality since Kotlin 1.2:

The Kotlin standard library is now fully compatible with the Java 9 module system, which forbids split packages (multiple jar files declaring classes in the same package).

Well, lets to my problem: I'm getting this when I try to execute gradle build task:

04:29:27: Executing task 'build'...

Task :compileKotlin FAILED e: Module java.base cannot be found in the module graph e: Module java.management cannot be found in the module graph

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':compileKotlin'.

    Compilation error. See log for more details

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 0s 1 actionable task: 1 executed Compilation error. See log for more details 04:29:28: Task execution finished 'build'.

I searched for that error on Google and found this topic where a guy had the same problem, he said he fixed it by targeting a JDK greater than 8, but I'm already with JDK 11 as project SDK and sourceCompatibility.

I'll left some important parts of build.gradle and project structure here to you guys inspect it.

build.gradle

buildscript {
    ext.kotlin_version = '1.3.21'
    ...

    repositories { ... }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.17"
    }
}

plugins {
    ...
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'maven'


group 'io.github.dogobot.core'
version '4.0.0'

...

sourceCompatibility = 1.11

repositories {
    mavenCentral()
    jcenter()
    maven { ... }
    maven { ... }
    maven {
        url 'http://maven.ej-technologies.com/repository'
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    ...
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

module-info.java

module io.github.dogobot.core {
    requires kotlin.stdlib;
    requires kotlin.reflect;
    requires kotlin.logging;

    requires java.management;

    requires BootingBits;
}

I can run the project with no problems on Intellij Idea, just get this error when executing on gradle.

Project Structure

like image 356
NathanPB Avatar asked Feb 24 '19 07:02

NathanPB


3 Answers

I changed my gradle jdk from JDK 8 to JDK 11 and now it works like a charm.

like image 58
NathanPB Avatar answered Nov 17 '22 00:11

NathanPB


File -> Invalidate caches -> select "Clear file system cache and Local history" -> Invalidate and Restart

Solved it for me...

like image 45
RoyalBigMack Avatar answered Nov 16 '22 23:11

RoyalBigMack


If changing JDK version doesn't work try the Repair IDE option in IntelliJ

File -> Repair IDE

like image 2
Adarsh D Avatar answered Nov 17 '22 01:11

Adarsh D