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

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.

Related

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

Micronaut Kotlin: Rest Controllers not working

I'm creating a simple application with 2 Rest Controllers with Kotlin. However, for every endpoint
the error is
{"message":"Not Found","_links":{"self":{"href":"
My inferences till now, for which I have searched for solutions and they didn't worked.
1.The generated folders are also empty. Might be an issue
As I have tried both IDE and command line. So, ruling out any issue from Idea
Tried to add a Java Controller in the project, but that also didn't run
Tried to use Micronaut annotations along with Spring Web annotations, didn't worked.
Is there anything else that I can change to make it work?
Ideally I would want to make it work with Kotlin and not Java.
Environment:
IDE - IntelliJ Idea 2020.2 Ultimate
Build - tried both Maven and Gradle, command line and IDE
EnableAnnotations: Done
Delegate Gradle Runner: Done
JDK: 11 ( but set as 8 in build file)
Update1 - Got few things working
Got the base code from Micronaut Launcher site
My Sample code
#RestController
#Validated
#RequestMapping("/hello")
open class UserController {
#Get("/echo")
fun echo():String{
return "hello"+ System.currentTimeMillis()
}
}
AOP is not working and thus had to use #Validated and open. The build.gradle has allOpen plugin.
#GetMapping should work as per https://micronaut-projects.github.io/micronaut-spring/latest/guide/#springMvc
I had to use Micronaut's #Get to make it working
Please help in fixing these 2 issues.

Kotlin, IntelliJ: math operator not working

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.

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.

Kotlin - Error: Could not find or load main class _DefaultPackage

I followed the Kotlin tutorial for eclipse here : Getting Started With Eclipse Luna
However, I'm running into this error:
Error: Could not find or load main class _DefaultPackage
Anyone who knows to get around this?
This was a severe bug (KT-10221) in automatic generation of Launch Configuration in plugin version 0.4.0. It was fixed in 0.5.0 so the recommendend way to workaround is to update plugin.
The source of the problem was that the plugin used an old pattern for generating name of the class for main function that had been abandoned by Kotlin compiler.
It's possible to workaround it by editing launch configuration (Eclipse Menu -> Run -> Run Configurations...) by hand and changing Main class field in Java Application group. If the file is named hello.kt with no package directive, as it is described in tutorial, than corrected string should be HelloKt.
If file has name other.kt with package my.tutorial than the Main Class should contain my.tutorial.HelloKt. You can read more about it in the section Package-Level Functions of Calling Kotlin From Java page.
I have been getting the same issue. And after putting the right compiler output path, it got resolved.
Go to Project -> Project Compiler output :
In the text box, fill this:
[Absolute Path]/{Project Name}/out
In my case I was having this problem while trying to run the program using the Application Gradle plugin. The problem was in the mainClassName property using single quotes instead of double ones
This didn't work:
mainClassName = 'demo.HelloWorldKt'
With double quotes, it works:
mainClassName = "demo.HelloWorldKt"
For me it worked after I installed the correct JDK. I first had JDK 11 but the tutorial I did was with JDK 8 so after I installed this and set it in the "installed JREs" options it found the main class without having any "mainClassName" or any other option in the build.gradle file.
For me, it worked in a fresh eclipse workspace. Possibly, the Kotlin eclipse plugin is not playing well with other plugins (in my case, PyDev).
I'm creating a Kotlin Application with JavaFX and I had this issue until I went to:
Run > Run Configurations > Java Application > Common
I unticked "Allocate console" and it fixed the issue.