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?
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.
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.
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
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.
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.