How to solve IntelliJ – Error: Could not find or load main class
How to solve IntelliJ - Error: Could not find or load main class
This problem happens to me frequently, then I did a lot of research. Eventually, I find out the way to solve it. The problem is that when I look at the configurations, I cannot find the src folder in it nor choose my main class from the list neither.
In summary, you need to:
- Mark your src directory as “Sources Root”
- Cancel your src’s exclusion
- Delete your .idea folder and re-add your module in “Project Structure”. After this, try 1, 2 again
- Rebuild your project, or delete your current build and add a new one
If you don’t know how to do so, I will explain them in detail, just keep reading.
First attempt: here, we can mark our src folder as one of the “Sources Root”, which you can do by right click on the src folder on your file tree.
Second Attempt: if the “Sources Root” doesn’t work for you, you still can try to “Cancel Exclusion” from the same option here:
Third Attempt: if both solutions above cannot help you, then I think you are having the same issue as me, which took me a while to figure it out. Now you can try to delete your .idea folder and then add a new module in the “Project Structure” if they are gone and re-try 1, 2 again. IMPORTANT: make sure you back up all your project files before this action.
Last but not least: This is optional, in case it still doesn’t work here – afterward, also try to rebuild your project, which you can do from the Build option on top, or you can delete your current build and add a new one. A quick way, you can delete your build and then just run the main method, the IDE may automatically configure for you. This solution works well for me, so I guess it will do the same job for you too.
That’s it, I hope my solution helps you to fix this problem.