Kotlin - Application project template - error main class not found - kotlin

I am using IntelliJ IDEA 2021.1 (Community Edition) - Build #IC-211.6693.111, built on April 6, 2021.
I created a new kotlin project - project template(JVM): Application, built system: Gradle Kotlin,
project JDK:1.8 version 1.8.0_281.
In the “scr/main/kotlin” folder, I created a kotlin file with a simple code like "fun main(){println(“Hi!”)}.
When I tried to run the file(the build was ok), I got this error: “Could not find or load main class MyAppKt”.
What did I do wrong?

Did you also add your main class to the build.gradle file?
mainClassName = "projectPackage.MyAppKt"
I assume if you are adding the MyAppKt file manually it is not added to the build.grale. But clearly more information about the project would help here. Meanwhile check this post Kotlin - Error: Could not find or load main class _DefaultPackage

Related

HelloWorld in Kotlin gives error "Could not find or load main class"

I spent the last 1,5 hour trying to make this simple tutorial work in IntelliJ IDEA, as you can see in this video.
When trying to run the code, I get the error:
/[...] -Dfile.encoding=UTF-8 src.HelloKt
Error: Could not find or load main class src.HelloKt
Caused by: java.lang.ClassNotFoundException: src.HelloKt
I have tried setting up SDK, invalidating cache, removing .idea and .gradle, rebuilding project, deleting the profile and adding it again. I tried those actions in different orders.
Here's a screenshot of the project:
It also complains Kotlin is not configured, but I have already configured it.
Here's the run configuration:
Here are the project settings:
Your Hello.kt file needs to be somewhere inside the src/main folder, probably in src/main/kotlin. This is different from the tutorial, because your project is using Gradle, and the one in the tutorial isn't. I think this is because newer versions of IntelliJ use Gradle by default for new projects, which wasn't the case when the tutorial was written.
The use of src/main/kotlin and src/test/kotlin as source code directories is a convention in Gradle (and Maven). When importing a Gradle project into IntelliJ, main becomes a module, and kotlin becomes a source folder within that module. The same goes for test. In your screenshots, the bold text and blue icons on main and test confirm that's how your project is set up. Files outside of those folders aren't treated as source files, which explains why your Hello.kt file isn't being compiled or recognised correctly.
It's likely that the default behaviour of IntelliJ when creating a new project has changed since this tutorial was written. In the tutorial, they select "Kotlin" as the project type and this creates a project that doesn't use Gradle. As a result, the project doesn't use the src/main/kotlin directory structure.
I can see from your video that you selected the same option, but on the next screen, IntelliJ still automatically selected Gradle as the build system for the new project. To match the project structure used in the tutorial, I think you would need to select "IntelliJ" as the build system.

JavaFX can't build artifact - fx:deploy is not available in this JDK

I have a JavaFX project that I would like to build as a Jar-file. However, when I attempt to do so, I get an error.
Error:Java FX Packager: Can't build artifact - fx:deploy is not available in this JDK
I found a similar problem on here from last year, but it seemed like they concluded nothing.
This happens because either you have many JDKs installed and compiling by another and running by another or you are using the Javafx Application jar feature when creating artifacts in Intellij which is unfortunately broken. Before proceeding with the below steps make sure that you are compiling with and running with the same JDK version.
Here is you fix it:
1 - Create a Launcher class:
The Launcher class is going to call the main JavaFx class from which your appliaction runs. Choosing to make the Jar directly through the Main class is going to error out giving the following error:
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
Your Launcher class should look something like this:
public class Launcher {
public static void main(String[] args) {
MainGUI.main(args);
}
}
2 - On to building the Jar
You probably still have a META-INF folder from the previous build so delete it.
Build the project as a JAR:
File->Project Structure -> Artifacts -> "+" -> JAR-> from modules with dependancies..
Choose the Launcher class for you main and check "copy to the output directory and link via Manifest" and Press Ok
Press Apply then OK.
go to Build -> Build artifacts-> Rebuild
In the JetBrains website I found a good article about, Package JavaFX applications which was really helpful. In the #troubleshoot section it says that,
Error:Java FX Packager: Can't build artifact – fx:deploy is not available in this JDK
The fx:deploy task was a part of the Ant plugin that was formerly
distributed in ant-javafx.jar as a part of Java Packager. The Ant
plugin is not included in jpackage in the current JDK versions.
If you're using a JDK build of version 9 and later, use third-party
solutions for packaging. For example, refer to section Runtime images
in the JavaFX official documentation.

How to include an Intellij gradle project inside another Intellij gradle project?

I have two java projects as Bukkit/Spigot plugins. Both projects are using gradle, private repositories, and one project should inherit from another.
Projects:
SpigotCore - Contains database management and utility classes. This is the "main" project.
Minigame Framework - Runs minigames. Needs database access and utility class access.
What is the best way to make the Minigame Framework inherit from the SpigotCore project using gradle? I have been unsuccessful in getting Intellij module dependency working. Any and all help is appreciated!
I solved this issue by just compiling the SpigotCore and then including the jar file in my build.gradle file.
Answer used: https://stackoverflow.com/a/20956456/2865125
Now following the answer above, it shaded the SpigotCore classes into the MinigameFramework. This is not what we want since both projects are Bukkit plugins and will be provided at runtime. So I changed "compile" to "provided" and it works great!
The change:
dependencies {
provided files("somePath/spigotcore.jar")
}

Error:Could not find property 'semanticVersion' on root project

My IntelliJ java Gradle project failed with the error.
Error:Could not find property 'semanticVersion' on root project 'MY_PROJECT_NAME'.
It did not create src and other folders.
I've got this error when trying to create a new Gradle Java project. After I have created project with auto folder generation option in IntelliJ wizard, I did not see any of the source or grade wrapper folders. Also, the project build failed everytime with error
Error:Could not find property 'semanticVersion' on root project 'MY_PROJECT_NAME'.
The final solution was that there was no "gradle.properties" file in my project. After I have created a new file "gradle.properties" and added line 'semanticVersion=1.0.0' to it, everything worked fine. Hope this helps someone.

Play Framework 2.4 and IntelliJ Idea

I am trying to open a play 2.4 project in IntelliJ but since things have changed I don't know how to do this.
In previous versions I could just run
activator idea
Or use the activator UI and click on generate intelliJ project, but in 2.4 the idea command doesn't seem to exist
[error] Not a valid command: idea (similar: eval, alias)
[error] Not a valid project ID: idea
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: idea (similar: clean)
[error] idea
[error] ^
And the UI seems broken, when I click on generate intelliJ project it tries to compile the app and gives this error:
play/Play$
java.lang.NoClassDefFoundError: play/Play$
Use 'last' for the full log.
Failed to load project.
I created the project from scratch using the play java template with:
activator new
I have also tried importing the folder as a project but intelliJ doesn't seem to identify it as a project
I run into the same problem, as I used IDEA to open a project folder, it had an play 2 app in a sub folder, then I import module (play 2 app) to the system.
And it works well.
After that I have changed the module folder name, then when I run the app, it displayed:
Not a valid project ID: "project team"
I re-checked the folder, and found that in the File -> Project Structure option, the name of module is "root", and the "team" is the module for whole project (not the module imported by SBT), so apparently, the module wasn't functional after I changed the module folder name.
So I found and removed all .idea folder, which is IDEA configureation, then re-open/re-import the module, still not work. I thought it's IDEA cache issue, it do have cache for the opened project, so I changed the project folder from team to something else, clean the .idea folders, and re-open/re-import it. It worked.
If the play app is in the project folder as a sub folder, to import the module at File -> Project Structure.
The project name should be "root" when running it in IDEA. So in this case, you should rename "project team" to "root" in the name field.
Solution 1
In my case (IDEA 2018.2), I changed the lazy val variable in build.sbt, it had the name "root" when my project name was "top", changed "root" to "top".
Before:
lazy val root = (project in file(".")).enablePlugins(PlayScala)
After:
lazy val top = (project in file(".")).enablePlugins(PlayScala)
Change project variable
Solution 2
The method proposed by Tom solved my problem partially, because after rebooting the IDEA I returned the project name back, it was necessary to change the variable from "top" to "root" in build.sbt.
Before:
name: = "top"
After:
name := "root"
Intellij IDEA lets you quickly create a Play application without using a command prompt. You don’t need to configure anything outside of the IDE, the SBT build tool takes care of downloading appropriate libraries, resolving dependencies and building the project.
Before you start creating a Play application in IntelliJ IDEA, make sure that the latest Scala Plugin is installed and enabled in IntelliJ IDEA. Even if you don’t develop in Scala, it will help with the template engine and also resolving dependencies.
Basically, install Play Framework, Scala plugins and import project into Intellij as SBT project. Don't use activator to create IDEA project files. More details here.
It seems I had not updated scala/sbt to the latest version in intelliJ
Once I had done this it noticed that it was a valid project, though the docs don't seem to mention you can import it as an SBT project, just how to create it as a new sbt project (which I did not want to do as I wanted to create it via activator)
I also had the project/play-fork-run.sbt file issue
I use Intellij Idea 16.3.3 and this problem appears sometimes. How I fixed it? You just need to open your SBT plugin and under your project get the context menu then you should choose "Ignore SBT project". After a current process is finished you should turn on this option again. This is work for me:)