Kotlin: Unresolved reference: java

Just getting started with Kotlin (1.2.21 plugin) in IntelliJ IDEA (2017.3.4) using version 9.0.4 of the JDK. I’m using the IDEA build tools. Wrote a simple class as follows that starts with the following line:

   import java.time.LocalDateTime

The compiler says “Error:(1, 8) Kotlin: Unresolved reference: java”. Is this a Kotlin thing or am I missing something in the IDEA configuration?

1 Like

This code works in the same environment as yours

import java.time.LocalDateTime

fun main(args: Array<String>) {
    val date = LocalDateTime.now()
    println(date)
}

Can you give more information about the error, or maybe show more code?

1 Like

Make sure your Project SDK in IntelliJ is set up correctly.

1 Like

SackCatellon,

Your code doesn’t compile in my environment (same error). See the compiler messages below. (I don’t know the reason for the Javascript warnings.)

Information:Kotlin: kotlinc-js 1.2.21 (JRE 9.0.4+11)
Information:2/22/2018 12:14 PM - Compilation completed with 2 errors and 7 warnings in 2s 610ms
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/kotlinc/lib/kotlin-stdlib.jar’ is not a valid Kotlin Javascript library
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/kotlinc/lib/kotlin-reflect.jar’ is not a valid Kotlin Javascript library
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/kotlinc/lib/kotlin-test.jar’ is not a valid Kotlin Javascript library
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/kotlinc/lib/kotlin-stdlib-jre7.jar’ is not a valid Kotlin Javascript library
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/kotlinc/lib/kotlin-stdlib-jre8.jar’ is not a valid Kotlin Javascript library
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/kotlinc/lib’ is not a valid Kotlin Javascript library
Warning:Kotlin: ‘C:/Users/Servant/.IdeaIC2017.3/config/plugins/Kotlin/lib’ is not a valid Kotlin Javascript library
C:\Users\Servant\Documents\Projects\Sample\Code\src\Sample.kt
Error:(1, 8) Kotlin: Unresolved reference: java
Error:(4, 16) Kotlin: Unresolved reference: LocalDateTime

Nanodeath,

I’ve examined the project settings at length, but I may lack the experience with Kotlin and IDEA to know what to look for. The plugin for Kotlin is installed, and the Kotlin compiler is configured to use the correct version of the plugin.

Looks like you are using Kotlin2Js. If you are using idea to compile the code, make sure your project settings are set to Kotlin(JVM) and not Kotlin Java Script.

1 Like

Wasabi375,

I don’t see anywhere in the project settings to do that explicitly. I created a new project and selected KotlinJVM in the setup wizard and the code compiles fine now.

Thanks,

Servant

Hello, is there a reason why java can’t be imported in a kotlin file is a Kotlin/Js project? I am trying to build a simple frontend to my already written kotlin files in which I use java libraries but I get this unresolved reference error, not sure what the problem is

Kotlin/JS and Kotlin/JVM are completely different targets. Java libraries can only by used in JVM projects.

1 Like

Do you recommend anything in order to implement a frontend? I found that kotlin/JS is the simplest way since my project is really not that complicated. I don’t want to use something like kolin/React. I have found very little resources about frontend with kotlin unfortunately.

You can try https://kvision.io (disclaimer: I’m the author of this framework)

2 Likes

Thank you! Will using Kvision still require me to not import java in my kotlin files though?

Yes, it does.
If you absolutely need to use JVM code (e.g. your business logic is already implemented as a JVM library), you have two options if you want to stay with Kotlin.
First - build fullstack application with Kotlin Multiplatform and split your code between the frontend (Kotlin/JS) and the backend (Kotlin/JVM). KVision has great support for building fullstack apps.
Second - use a JVM only framework (e.g. Ktor, Jooby, Javalin, Spring Boot, Vaadin, Kweb) and create typical server side application.

1 Like

Sounds great I think I will stick with the multiplatform idea then. Thanks again!