Could not find or load main class with IntelliJ Application configuration

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.


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