21

I've been trying to set up a new Kotlin project in IntelliJ IDEA, using the default IntelliJ Build System (not Gradle) and JDK 15. Whenever it creates the default main.kt file, and I try to build and run the main function, which looks like this:

fun main(args: Array<String>) {
    println("Hello World!")
}

the build fails after a short time with the following errors:

Kotlin: Cannot access built-in declaration 'kotlin.Array'. Ensure that you have a dependency on the Kotlin standard library

Kotlin: Cannot access built-in declaration 'kotlin.String'. Ensure that you have a dependency on the Kotlin standard library

and

Kotlin: Unresolved reference: println

I've tried changing the project SDK to the kotlin SDK, to Java 14 and to Java 1.8. I'm not sure exactly how to add the dependency it asks for. Any help is appreciated.

This is what the dependencies window shows within the project: Module Dependencies

Right now the project is using JDK 15, and Kotlin 1.4.10. I've tried updating the plugin, uninstalling and reinstalling, deactivating and reactivating, all to no avail.

A. P. Roblem
  • 455
  • 1
  • 3
  • 10

10 Answers10

31

Try enabling KotlinJavaRuntime

  1. Open Project Structure window (File > Project Structure)
  2. Go into Modules
  3. Go to Dependencies tab
  4. Tick the Enable KotlinJavaRuntime checkbox
Jonas Borggren
  • 2,591
  • 1
  • 22
  • 40
vbevans94
  • 1,500
  • 1
  • 16
  • 15
11

Removing the .idea/library file inside the project directory and then selecting "use library from plugin" when prompted to configure Kotlin files by the IDE solved the problem.

A. P. Roblem
  • 455
  • 1
  • 3
  • 10
4

Please provide more information and check what Kotlin version do you have installed:

  1. Tools > Kotlin > Configure Kotlin Plugin Updates: install the latest version of Kotlin

  2. File > Project Structure > Project: what is your Project SDK there? Choose 15 (something line java version "15.0.1"). If you don't have 15 there, click "+" and add the newest version (unless you need an older version).

JetBrains
  • 456
  • 2
  • 6
  • 18
2

Probably you need to add KotlinJavaRuntime library in you project. Please see paragraph "Create Kotlin files in Java projects" in https://www.jetbrains.com/help/idea/mixing-java-and-kotlin-in-one-project.html After configuration you should see needed jars in lib folder in your project. Rebuild you project. Make also File->Invalidate Cache/Restart.

1

I tried all of the above but eventually found out, that in one project, my SDKs were completely broken after the newest Intellij Update. If you go to Project Structure -> Platform Settings -> SDKs, I had an entry for Kotlin with a non existing path for an Intellij Version of last year. This was definitely not there before and it seems to have messed up the kotlin installation. After i deleted that entry and as described above the libraries as well, it worked again.

Loop
  • 480
  • 2
  • 9
1

For me this helped:

  1. Close the project
  2. Delete idea. folder
  3. Open project
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
1

In this case, it was a misconfigured IntelliJ instance.

Project Structure -> SDKs -> kotlinSDK -> + -> ok

rebuild will be work fine

1

In my case the Kotlin Standard Library JDK extension was missing. https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib-jdk8

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jdk8</artifactId>
    <version>1.7.21</version>
</dependency>
dodomku
  • 21
  • 3
0

I have a Ubuntu 16.04 with IntelliJ 2020.3 newly installed. I run to the same issue here.

To solve this issue, I made a new kotlin file in my project. Then when the system was trying to add this new Kotlin file, you can find a notification related to "Configure project" on the right bottom corner of screen, click the notification. I get the following dialog.

Then check the Use library from plugin, this solve the issue. Use library from plugin

0

You can open files in folder ".idea" and see if there are closing tags </tagName> missing at the end of each file.

tananyGeek
  • 429
  • 4
  • 4