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

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

Related

Can't get kotlin "hello world" example to Run in intelliJ

I'm following the intelliJ IDEA tutorial titled Create your first Kotlin application
(Last modified: 08 March 2021). Here are the various versions I'm using:
~$ java -version
java version "16" 2021-03-16
Java(TM) SE Runtime Environment (build 16+36-2231)
Java HotSpot(TM) 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)
~$ javac -version
javac 16
Here's my code:
fun main(args: Array<String>) {
println("What is your name?")
val name = readLine()
println("Hello $name")
}
Here's what my project structure looks like:
I notice that it says "Kotlin not configured" above my code, but when I go to Tools > Kotlin > Configure Kotlin in Project, I get
:
In the Event Log pane, I see this:
4/15/21
1:54 PM Download pre-built shared indexes
Reduce the indexing time and CPU load with pre-built JDK shared indexes
Always download
Download once
Don't show again
Configure...
1:54 PM Gradle sync started
1:54 PM Gradle sync failed: Unsupported class file major version 60 (5 s 329 ms)
1:55 PM No IDE or plugin updates available
In the Problems(?) pane, I see this:
Unresolved reference: println
Unresolved reference: readLine
Unresolved reference: println
Parameter 'args' is never used
Package directive doesn't match file location
Any ideas on why the basic functions in my program are not recognized?
Thanks for looking!
Edit: I tried downloading JDK 15 and specifying that in my Project Structure, but I got the same results:
try build>clean project.
As for your unresolved references, you can double-click/left-click > show context options. If that doesn't work then maybe reinstall InteliJ Idea or download Android Studio which has a lot more support on google and yt.
Okay, I got it working. I'll try to figure out what I did.
tldr; I had to switch from JDK 16 to JDK 15 to get my hello world console application to run.
First, when I was setting up the project, I chose the wrong Project Template: I selected JVM - Application rather than JVM - Console Application. This is what I needed to do:
So, I started a new project, chose the right template, and I typed in my code, and I noticed a Build pane at the bottom left displayed:
I tried Build > Build Project, and nothing changed. So, I tried the Run menu item: the first section of the Run menu was grayed out, but there was another Run command in the second section of the Run menu. I selected that Run, and a little window popped up with the following:
Run
0 Edit Configurations
The "0 Edit Configurations" line was highlighted, and I got nowhere trying to figure out how to Run my code.
So, I started yet another new project, and this time I selected a different JDK, version 15:
After clicking Next, then Finish, I typed in my code, and this time the Build pane looked like the following (without me doing anything but typing in my code):
After I finished typing in my code, as the tutorial described, this time there was a little green arrow in the gutter:
When I clicked on the green arrow in the gutter, I could successfully Run my program.
So, it appears that you cannot use JDK 16, and you have to use JDK 15. intelliJ was able to detect all my installed Java versions, so any version you have installed should appear as a choice in the the project settings. You may have to quit intelliJ, then restart intelliJ after you install JDK 15 in order to see it listed as a choice under Project JDK: when you create a project.

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 Error:java: java.lang.ExceptionInInitializerError

Every time I encounter this exception in IntelliJ, I fix it trivially and forget the fix easily.
Code:
package whatever;
import org.junit.Test;
public class TestClass
{
#Test
void test() {}
}
Scenario:
Add new TestClass.
Right-click TestClass.
Select "Run 'TestClass'" to run test cases.
The "Messages Build" pane shows:
Information:javac 9-ea was used to compile java sources
Information:Module "dummy" was fully rebuilt due to project configuration/dependencies changes
Information:8/16/17 11:35 PM - Compilation completed with 1 error and 0 warnings in 1s 663ms
Error:java: java.lang.ExceptionInInitializerError
What can possibly go wrong?
What are the likely issues in this simple scenario?
IntelliJ: COMMUNITY 2017.1 (idea-IC-171.4424.56)
To fix the issue, I do:
File -> Project Structure... -> Project Settings / Project -> Project SDK.
Change from "9-ea" to "1.8".
DETAILS
Apparently, the issue is discrepancies in selected JDK-s to build (java 9) and run (java 8).
I'm not sure how "9-ea" gets re-selected there for the same project - neither IntelliJ itself runs in "9-ea" JRE (according to Help -> About) nor JAVA_HOME env var is set to it nor other possible settings (like Maven -> Runner) suggest any "9-ea".
I also didn't manage to run the test under the same JDK (java 9) which it gets compiled under. However, it's unclear what JDK tests are run under because IntelliJ reports only about JDK for compilation.
If you use Lombok: For me it was a solution to set the newest version for my maven lombok dependency in the pom.xml.
*<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.8</version>
</dependency>*
I was facing same error when i tried to run my application in IntelliJ-2019.2 version. Below are the steps i followed to resolve this issue.
Versions:
IntelliJ : IDEA-IntelliJ-2019.2
Java : jdk1.8_221
Go to below path in IntelliJ
File -> Project Structure -> Project -> Project SDK -> (select java version which you want to use )
(In my case under 'project SDK' java-11 was selected, I changed it to 'java8')
Click on 'Apply' and then 'OK'.
I feel I ran into this issue because IntelliJ was trying to compile my java classes using in-built java-11 whereas my java classes are built on java-8. So when i explicitly configured java-8 in IntelliJ, It worked!! Hope this helps.
I started seeing this exception once I installed Java 11 in my machine. JAVA_HOME was by default pointing to Java 11 and my project was still in Java 8. Changing JAVA_HOME to Java 8 jdk fixed the issue for me.
If you have multiple projects each running on a different JDK, use this command to temporarily change the Java version per command.
JAVA_HOME=/path/to/JVM/jdk/Home mvn clean install
If you have recently updated your IDE then you can try these steps.
Delete .idea directory for the idea project/workspace
Then go to File -> Invalidate Caches / Restart...
Once Idea is restarted re-add/import your module(s)
I faced a similar issue with JARs and Jena (while run from IntelliJ it works).
I was using Apache Jena v4.0.0 in my project and have built a JAR (with a main class for the JAR to act as a console app).
The JAR builts successfully with IntelliJ but when run throws java.lang.ExceptionInInitializerError ... Caused by: java.lang.NullPointerException. NPE suggests that something was not initialized properly.
The jar built with previous version Jena 3.17.0 works perfectly.
What I did to fix it
I've opened both the JARs, compared their META-INF folders and encountered the difference in
my.jar\META-INF\services\org.apache.jena.sys.JenaSubsystemLifecycle
The new version (jena v4.0.0) contains only one line:
org.apache.jena.tdb.sys.InitTDB
The old version (jena v3.17.0) contains two different lines:
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ
I've added the old two lines to the file and repacked new JAR with it:
org.apache.jena.tdb.sys.InitTDB
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ
It resolved my issue.
Update: recent Jena v4.4.0 builts with the same "bug".
I'm not an expert and there is probably a better way than patching a JAR by hand.
But I still hope that this solution will help someone like 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.

What's the reason for "Error:Cannot determine Java VM executable in selected JDK"?

I am using IntelliJ IDEA 13.1.4 and also tried the latest release 14.
Running SBT I get the following error:
Error:Cannot determine Java VM executable in selected JDK
I have JDK 1.7 installed on my machine and on PATH.
In the logs (~/Library/Logs/IntelliJIdea14/idea.log on MacOS) there's the following stack trace:
2014-11-03 11:22:05,054 [4896641] WARN - nal.AbstractExternalSystemTask - Cannot determine Java VM executable in selected JDK
com.intellij.openapi.externalSystem.model.ExternalSystemException: Cannot determine Java VM executable in selected JDK
at org.jetbrains.sbt.project.SbtExternalSystemManager$$anonfun$10.apply(SbtExternalSystemManager.scala:97)
at org.jetbrains.sbt.project.SbtExternalSystemManager$$anonfun$10.apply(SbtExternalSystemManager.scala:97)
at scala.Option.getOrElse(Option.scala:120)
at org.jetbrains.sbt.project.SbtExternalSystemManager$.executionSettingsFor(SbtExternalSystemManager.scala:96)
at org.jetbrains.sbt.project.SbtExternalSystemManager$$anonfun$getExecutionSettingsProvider$1.apply(SbtExternalSystemManager.scala:54)
at org.jetbrains.sbt.project.SbtExternalSystemManager$$anonfun$getExecutionSettingsProvider$1.apply(SbtExternalSystemManager.scala:54)
at org.jetbrains.sbt.package$$anon$3.fun(package.scala:29)
at org.jetbrains.sbt.package$$anon$3.fun(package.scala:28)
at com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil.getExecutionSettings(ExternalSystemApiUtil.java:590)
at com.intellij.openapi.externalSystem.service.ExternalSystemFacadeManager.a(ExternalSystemFacadeManager.java:201)
at com.intellij.openapi.externalSystem.service.ExternalSystemFacadeManager.a(ExternalSystemFacadeManager.java:178)
at com.intellij.openapi.externalSystem.service.ExternalSystemFacadeManager.doInvoke(ExternalSystemFacadeManager.java:133)
at com.intellij.openapi.externalSystem.service.ExternalSystemFacadeManager$MyHandler.invoke(ExternalSystemFacadeManager.java:270)
at com.sun.proxy.$Proxy57.getResolver(Unknown Source)
at com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask.doExecute(ExternalSystemResolveProjectTask.java:48)
at com.intellij.openapi.externalSystem.service.internal.AbstractExternalSystemTask.execute(AbstractExternalSystemTask.java:137)
at com.intellij.openapi.externalSystem.service.internal.AbstractExternalSystemTask.execute(AbstractExternalSystemTask.java:123)
at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$2.execute(ExternalSystemUtil.java:475)
at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$3$1.run(ExternalSystemUtil.java:543)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:609)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$7.run(ProgressManagerImpl.java:410)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$3.run(ProgressManagerImpl.java:194)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.a(ProgressManagerImpl.java:281)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:233)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:181)
at com.intellij.openapi.application.impl.ApplicationImpl$10$1.run(ApplicationImpl.java:640)
at com.intellij.openapi.application.impl.ApplicationImpl$8.run(ApplicationImpl.java:405)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jetbrains.ide.PooledThreadExecutor$1$1.run(PooledThreadExecutor.java:56)
What can be the reason for this?
You should be able to work it around by setting the jdk from the settings not from the open/import project dialog.
From the welcome screen, go to Configure -> Project defaults -> Project structure and add the jdk.
Opening the sbt project should work well then.
Found solution from here
Another way to set JDK is from your current module/project settings (for your current project)
PickOpen Module Settings from project context menu (or default hit F4), then from left tab select Project and point correct Project SDK on dropdown.
The issue is usually caused by a wrong JDK version in ".idea/sbt.xml", e.g.:
<option name="jdk" value="1.7" />
This option is not updated accordingly when the Project SDK is changed, see SCL-10085. If you have the other JDK (1.7 in my example) generally configured, no error will occur, but the Project SDK will silently be changed back. Otherwise, this error occurs.
The problem can easily be resolved by manually editing the value in ".idea/sbt.xml" to the right JDK version.
Same error also occurs when you try to do a refresh in "SBT tasks".
Open
Preferences -> Language & Frameworks -> Scala Compiler Server
Turn on
Run compile server (in external build mode)
Once you done with refreshing the project, turn it off again to enable hotswapping back when you change your code in the editor.
I had to open Settings -> Language & Frameworks -> Scala Compiler Server
Then set the JVM SDK there, which was <No SDK>.
This was in addition to setting the Project SDK in Project Structure -> Project.
See the screenshot here.
IntelliJ 13.1.6 > File > Project Structure > set Project SDK
I got the same problem after I delete Java1.6 and Java1.7 from Project Settings(with Java8 as default).
Finally I solve the problem by change SBT JVM config to Custom Java(Settings -> Build, Execution, Deployment -> Build Tools -> SBT).
For me the above suggestions did not help for some reason. However, I did figure out that under Project Defaults > Project Structure (Welcome screen) my default Project SDK was set to the Go SDK.
What worked for me was setting this default SDK to the Java JDK.
For me, I had selected the JDK in "Open Module Settings" > Module > Dependencies > Module SDK.
However, SBT was looking for JDK at project level which is set in "Open Module Settings" > Project > Project SDK as #michasm has pointed out above
The latest Nightlies of the Scala plugin change how the project JDK is set, which should solve this in most cases. Let me know if it still breaks on some cases.
This happened to me in a multi-language project when my primary module's Project SDK was Python and I was trying to add a secondary module that was JDK (importing an sbt project).
I had to temporarily switch the primary module's Project SDK to JDK in order to add the sbt module. I then had the ability to go back and change each module to the correct SDK.