Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find or load main class with IntelliJ Application configuration

I have a project in IntelliJ IDEA 2019.1.3 using Gradle, with a main class like this:

public final class Main {
    public static void main(String[] args) {
        // Do stuff
    }
}

But every time I try to launch my program, it always shows the same error:

Error: Could not find or load main class com.example.Main
Caused by : java.lang.ClassNotFoundException: com.example.Main

I'm using the Application configuration template. I have tried the following:

  • Clean/Rebuild project
  • Invalidating caches/restart
  • Reimport Gradle project
  • Deleting the .idea folder
  • Deleting and recreating the configuration profile
  • Recreating the project from scratch
  • Completely reinstalling IntelliJ
  • Updating from JDK 8 to JDK 11
  • My source sets are correct, the build folder, the classpath module, the file package are correctly set. The build task is run before launching. In fact, there's effectively a Main.class file under the build/ folder and in the generated jar file.

The only solution I know is to run with a gradle task:

task run(type: JavaExec) {
    main = "com.example.MainKt"
    classpath = sourceSets.main.get().runtimeClasspath
    standardInput = System.in
    isIgnoreExitValue = true
}

But I'd rather not do that since the console won't accept input, doesn't support unicode for some reason, and I can't pass program arguments as easily as with the IntelliJ's configuration window.

Has anyone had this problem and how was it fixed? It's only been happening to me since I updated to IntelliJ 2019.1, it worked fine most of the time on 2018.3.6.

EDIT: Gradle settings enter image description here

EDIT 2: The original question was about Kotlin but I realized I get the same error with Java.

like image 946
Nicolas Avatar asked May 25 '19 13:05

Nicolas


2 Answers

When trying to reproduce your libgdx example the very first time everything worked. The second attempt to freshly import the project and take screenshots along the way failed like yours. From then on it kept failing.

Apparently somehow IntelliJ gets the classpath wrong when importing this project from gradle. It looks for build/java/main instead of build/kotlin/main.

To fix the issue Open the Module Settings (F4) of the project and change the "module compile output path" of the modules desktop and core to the kotlin output path. Just replace the word java in the path with kotlin:

module settings

When you then hit the "run" button next to the main method, it fails like this:

Caused by: java.lang.IllegalStateException: GLFW windows may only be created on the main thread and that thread must be the first thread in the process. Please run the JVM with -XstartOnFirstThread. For offscreen rendering, make sure another window toolkit (e.g. AWT or JavaFX) is initialized before GLFW.

This can be fixed by editing the launch configuration and add -XstartOnFirstThread to VM options.

The next attempt fails with this exception.

com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: libgdx.png

When changing the working directory in the launch configuration to the android/assets directory, just like you did in your gradle task, the demo will launch successfully.

For the record my environment:

OS: MacOs Mojave
JVM: openjdk version "1.8.0_212" (AdoptOpenJDK)
IntelliJ: 2019.1.3 Ultimate Edition
like image 192
Florian Gutmann Avatar answered Oct 17 '22 03:10

Florian Gutmann


The issue comes from the fact that the project has an Android module. The module needs the android gradle plugin which is globally applied to all modules in the project, even those which aren't android modules. When applied to a module, it adds an Android-Gradle facet that changes the module classpath from the default out/classes to build/classes/java. For kotlin modules, the main class can't be found at runtime since the classpath is wrong.

There's is currently no way to apply the android plugin to only android modules. Workarounds include patching the android.jar file, automatically removing the Android-Gradle facet on sync, or moving the android module to an external project. There's no clean way to do it.

The issue has already been reported here, but it shows no sign that it will ever be fixed.

like image 32
Nicolas Avatar answered Oct 17 '22 03:10

Nicolas