Could not find or load main class with IntelliJ Application configuration
Solution 1
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.
Solution 2
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
:
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

Nicolas
Updated on July 06, 2022Comments
-
Nicolas 6 months
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 thebuild/
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 2: The original question was about Kotlin but I realized I get the same error with Java.
-
Nicolas over 3 yearsYes that's it thanks a lot! (the
-XstartOnFirstThread
option is only needed by macOS users) Unfortunately the classpath are reset after each gradle sync... Also the classpath is not the same for all main methods. I have a main method in one of my tests, which is in the test module, so the classpath needs to be changed tokotlin/test
instead. Do you think there's a way for IDEA to set that automatically on run? -
Florian Gutmann over 3 yearsThe question is what the root cause of this really is. As stated above for me it worked on the very first import, so clearly there is some underlying issue somewhere. Have you managed to reproduce this issue outside of this project?
-
Florian Gutmann over 3 yearsDo you have installed and activated the android plugin in intellij? It seems that the import works for me when it is disabled.
-
Nicolas over 3 yearsYes I have the android plugin installed, I'll try to disable it. The issue happens in other projects as well, not just this one.
-
Florian Gutmann over 3 yearsWhen you uncomment the
:android
module insettings.gradle.kts
it also works with the android plugin enabled. Looks like there might be some incompatibility with the specific versions of android plugin, gradle and intellij. -
Nicolas over 3 yearsHaven't tried yet, but this appeared at about the same time as when updated gradle from 4.10 to 5.4 so it might be related.
-
Nicolas over 3 yearsThe android plugin seems to change the classpath of all modules from the default
out/production/classes
tobuild\classes\java\main
. When I go into the Project Structure window, I see that each of my module use theAndroid-Gradle
facet, but they are not android modules. I think this might be caused by the android gradle plugin being applied to all modules instead of just theandroid
module. I tried to define it directly inandroid/build.gradle.kts
but I couldn't find how.