Kotlin, IntelliJ: math operator not working - kotlin

Ubuntu Mate 20.04
IntelliJ IDEA Community 2020.2 EAP, installed through Snap
Kotlin 1.3.72, installed through Snap
JRE 1.8.0_242-8u242-b08-0ubuntu3~16.04-b08
Project set up as Kotlin: JVM|IDEA using SDK 11 (java 11.0.6)
Code:
fun main(args: Array<String>) {
var experiencePoints: Int = 5
experiencePoints += 5
println(experiencePoints)
}
has a red squiggly line under the "+=" and produces the following error:
Error:(3, 22) Kotlin: Cannot access 'java.io.Serializable' which is a supertype of 'kotlin.Int'. Check your module classpath for missing or conflicting dependencies
Changed the code to the following:
fun main(args: Array<String>) {
var experiencePoints: Int = 5
experiencePoints = experiencePoints + 5
println(experiencePoints)
}
Red squiggly line beneath the "+" character and same error.
This is Day 1 for me, following example in "Kotlin Programming, The Big Nerd Ranch Guide" by Skeen and Greenhalgh. No idea what to do about this error.

I had this in Intellij 2021.2 Ultimate on MacOS Catalina, and I did have the JDK installed. But in File -> Project Structure -> Project Settings -> Project -> Project SDK I had the Kotlin SDK selected. Makes sense, it's a Kotlin project, right? Wrong, it needs to be the Java JDK selected here.
Also under Platform Settings -> SDKs -> Kotlin SDK I was missing the Classpath. I'm not sure if this was necessary, but it was that way when I created a new Kotlin project from scratch. I ran into trouble importing an existing combined Java/Kotlin project.

I discovered that I didn't have default-jdk installed. Once I did, I found that there were extra options under File -> Project Structure -> Project -> Project SDK. I added user/lib/jvm/java-11-openjdk-amd64 java version "11.0.7" and the problem resolved.

Related

IntelliJ IDEA - gradle not recognizing gradle kotlin dsl in build.gradle.kts

I have a fresh install of IntelliJ IDEA 2022.2 (Community Edition), Gradle 7.4, and Kotlin plugin 222-1.7.10-release-334-IJ3345.118 that is not recognizing gradle kotlin dsl. I am trying to run a Ktor project that I generated on the ktor website--so I am fairly sure that the syntax is correct. I haven't made any other modifications.
The error e: /foo/build.gradle.kts:8:8: Unexpected tokens (use ';' to separate expressions on the same line)
Which is erroring out in
plugins {
application
kotlin("jvm") version "1.7.10"
id "io.ktor.plugin" version "2.1.0"
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10"
}
on the 4th line of that snippet.
Is there something special I need to do to configure Intellij to recognize that build.gradle.kts is gradle kotlin dsl and not Groovy?
Edit
As #aSemy pointed out, there is a Groovy vs Kotlin syntax mismatch that I had missed. Converting the first id to the second one fixed the syntax issue, but left me with a different issue
/foo/build.gradle.kts:8:25: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public infix fun PluginDependencySpec.version(version: String?): PluginDependencySpec defined in org.gradle.kotlin.dsl
public infix fun PluginDependencySpec.version(version: Provider<String>): PluginDependencySpec defined in org.gradle.kotlin.dsl
For my purposes, I decided to take out the library since it is described on Github to be for helping with Ktor deployments--but I'm only working locally.

Attempting To Run Kotlin Project In IntelliJ Idea But Getting Bug Error

My Previous Question + More
I am trying to start learning Kotlin, and I have installed IntelliJ Idea from JetBrains, I really like the layout of the IDE, and so I don't really want to change it.
Whenever I try to run the program, I get the following error:
Could not open init generic class cache for initialization script 'C:\Users\Jacob\AppData\Local\Temp\wrapper_init1.gradle' (C:\Users\Jacob\.gradle\caches\6.6.1\scripts\9x7j38tmeo1ohlyu8452ntfdm).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 60
I know it's not the code, and since I am not very familiar with IntelliJ Idea or Kotlin, I have no idea where to start looking for the error or where to fix it...
Someone said that this question's answers were the solution, but I looked their, tried what it shows, but still found now solution for my situation... When I follow what they say, I get the same error.
Someone please help!!! 😁
I created instruction:
Install JDK (for example Oracle JDK 14)
Open project folder with build.gradle and settings.gradle in Idea
Add JDK to project: press F4 on left Project Tool -> SDk -> Add -> Apply
Set new SDK to project: press F4 on left Project Tool -> Project -> Project SDK -> Set new SDK -> OK
Set new SDK to Gradle Build Tool: press Ctrl + Alt + S -> Find gradle in search -> Go to Gradle JVM -> Set new SDK -> OK
Create new directory src/main/kotlin/main.kt with code
fun main() {
val jdk = System.getProperty("java.version")
println("Using JDK: $jdk")
}
Go to main.kt in Editor and press Ctrl + Shift + F10 and you must see your JDK version in console like: Using JDK: 14.0.2
Take a look sample project

Updated intellij messed up Kotlin with 'Check your module classpath for missing or conflicting dependencies'

Lot's of frustration here. I decided to follow the pop-up prompting an intellij upgrade so it'd stop nagging me.
And now I get a compiler error on certain parts of the code:
Cannot access 'java.io.Serializable' which is a supertype of 'kotlin.String'. Check your module classpath for missing or conflicting dependencies
I can make a new kotlin project with only the following and run it:
fun main() {
println("helloWorld")
}
But if I change it to the following it does not compile:
fun main() {
println("hello" to "World")
}
$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
I'm on windows. I find it very challenging to debug these kind of things. It feels very similar to this and this upvoted one
It was solved by downloading a jdk and applying it to the project:
Click the plus, select a vendor and download:
And then apply it to the project:
And rebuild.
I encountered some similar problem on IntelliJ and end up with clicking reload all maven projects button.
Invalidate Cache and Restart helped

IntelliJ shows unresolved reference to java.lang.String#replace in Fabric Kotlin dev environment

class Foo(
val name: Identifier,
val trKey: String = "action.${name.toString().replace(':', '.')}"
// ^~~~~~~ this is unresolved
) {
// Members
}
The replace function is able to be resolved in Fabric's source code and it does run, but it doesn't in my Kotlin code.
I've tried setting the project SDK to 1.8, 11, and Kotlin SDK, and none of them seem to solve this issue. In fact, putting the SDK to 11 makes java.lang.String inaccesible.
I think I fixed it by adding KotlinRuntime into libraries through IntelliJ project structure (will get erased by Gradle import), or with gradle dependencies adding Kotlin library.
Instead I found changing JDK version back to 1.8 fixes this issue and is reproducable. The above only worked once for me.

Simple Intellij Kotlin project doesn't recognize LinkedHashMap or other collections

I installed the latest version of Intellij on Linux (2019.2.3 Community Edition) and added the Kotlin plugin.
I followed the Kotlin website's Getting Started with IntelliJ IDEA and created a hello world app. It worked fine until I added a LinkedHashMap. (BTW, I was running into this in a bigger project, but simplified it to this.)
This is all the code I added:
import kotlin.collections.LinkedHashMap
fun main(args:Array<String>) {
println("Hello world")
val map: LinkedHashMap<Int, String> = linkedMapOf(1 to "x", 2 to "y", -1 to "zz")
println(map)
}
I get this error on the map line:
Cannot access class 'java.util.LinkedHashMap'. Check your module classpath for missing or conflicting dependencies.
You can see I have 1.8 selected in the modules, and I verified java 8 is installed via terminal (java -version). What is the issue for this vanilla/default installation?
Ok, so I figured it out. For some reason, if I change the module to 11 (java 11) from 1.8 it just works.
Kind of weird that standard collections don't work out of the box, but... eh, I guess this works.