JetBrains
JetBrains

Reputation: 476

How can I fix "Unresolved reference: java" in Spring Boot Kotlin project?

I have created Spring Boot project with

I'm following this Tutorial: https://scotch.io/@grahamcox82/how-to-build-a-simple-rest-api-with-kotlin-and-spring-boot

I'm trying to

import java.time.Instant

in my Kotlin data class

And have an error

Unresolved reference: java

build.gradle.kts file:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.1.6.RELEASE"
    id("io.spring.dependency-management") version "1.0.7.RELEASE"
    kotlin("jvm") version "1.2.71"
    kotlin("plugin.spring") version "1.2.71"
}

group = "com.smight"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_1_8

val developmentOnly by configurations.creating
configurations {
    runtimeClasspath {
        extendsFrom(developmentOnly)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

Maybe I should install java library? How can I check this? Can anyone help please?

Upvotes: 4

Views: 17173

Answers (3)

Braian Coronel
Braian Coronel

Reputation: 22867

To get a more specific error you should first clean the autogenerated files

$ ./gradlew clean

In a modularized Spring project using Kotlin DSL the unresolved reference error could occur because the submodules are bootable.

build.gradle.kts (Project)

...

subprojects {

    ...

    tasks.getByName<BootJar>("bootJar") {
        enabled = false
    }

    tasks.getByName<Jar>("jar") {
        enabled = true
    }

}

GL

Upvotes: 1

JetBrains
JetBrains

Reputation: 476

The problem was that JDK was not correct found from IntelliJ

I solved the problem so:

  1. File -> Project Structure -> SDKs -> "+"
  2. Find the path to your SDK where it is installed
  3. New Project
  4. Copy/Paste
  5. Rebuild

Upvotes: -1

Natty
Natty

Reputation: 539

According to some research, this error can appear in this conditions :

  • You created a Kotlin2Js project instead of Kotlin JVM (source), try to recreate your project by selecting the right project type

or

  • You are using a Kotlin version that does not support JDK 11 (source), install JDK 8 instead and reconfigure your JAVA_HOME environment variable

It may as well be an error in your build.gradle file, copy/paste it in your question if the solutions above doesn't work

Upvotes: 3

Related Questions