Error: Could not find or load main class Main in Intellij IDE
Answered
Hello,
I'm new to Java and today I was trying to create a simple "HelloWorld" project in IntelliJ. Unfortunatelly when I try to run a project I got this error: Error: Could not find or load main class com.company.Main
Caused by: java.lang.ClassNotFoundException: com.company.Main
Here is a screen:

I search in stackoverflow https://stackoverflow.com/questions/42660125/java-lang-classnotfoundexception-when-running-in-intellij-idea/42660624#42660624 and intelij support forum. Change and add path, but nothing work for me.
When I try to compile a program from CMD everything is working fine. Could someone help me with it? It seems like some configuration problem in Intelij, but I cann't understand how to fix it.

Please sign in to leave a comment.
Hello,
Could you please check that compiled class is present in "out " folder. Is it possible to attach full classpath from the output?
Hello Yaroslav,
thank you for response.
Here is a screenshot from the "out" folder:
Classpath is:
And in project structure:
Sorry, I meant full classpath/command from Run tool window output. Also, could you please try to check the issue with new project without space in project name?
Hi @...,
having the same problem since IntelliJ 2022.3.x
(works perfectly with 2022.2.x or earlier)
May have something to do with the packaging change around the Main class
Here my run command:
Actually my problem is with a DevKit plugin run configuration. I may be in the wrong thread here...
Dan Cioca Please use https://intellij-support.jetbrains.com/hc/en-us/community/topics/200366979-IntelliJ-IDEA-Open-API-and-Plugin-Development for such questions.
Do you also have simple "Hello World" application?
Thanks for information! Please follow https://youtrack.jetbrains.com/issue/IDEA-302414/2022.3-EAP-SDK-issue-Run-configs-created-against-a-2022.3-EAP-SDK-dont-use-the-correct-classpath.
It's in progress now.
I had a similar problem with a new (maven) project that I created with IntelliJ. The solution was to restart IntelliJ and do a maven->clean
It's very easy to fix
Hi, I'm also running into the same issue, even with a new project created by default. I am pretty sure that my main class is set correctly. What other reason do you think it could be?
PutUrHeartOndaTable A few things to try to fix it:
Having the same problem since today (didn't touch any code or config, did start Intellij just like yesterday, can't find any of my main classes anymore).
Did follow Arina Efremova 's suggetions for PutUrHeartOndaTable and did first try invalidating caches → didnt work.
Resetting the IDE did also not help.
What now?
I did multiple restarts, did downgrade back to 2024.2.5, did re-import settings, reset, etc. Nothing helped.
Now (3h later), suddenly when I wanted to switch to Eclipse and I demonstrated it to a colleague it started working again, without any apparent reason (since still, nothin has changed in the repo).
Is it possible to find out why this happens in the future (special logs, etc?)
JP Please check https://stackoverflow.com/a/42660624/104891 .
Collect IDE logs / debug build logs per https://intellij-support.jetbrains.com/hc/en-us/articles/207241085 and contact support at https://intellij-support.jetbrains.com/hc/requests/new .
I actually fix that by moving the project from /Documents to /ProjectIdeas folder. Not sure why it is like that?
@PutUrHeartOndaTable From your shared screenshot, I saw you use a parent folder named “Sem1:2025” and I suspect this caused this issue.
Please do not use `:` in a parent folder for your source code. The `:` is a path separator character in the Java classpath causing the java think your project are having two classpath separated with `:`.
AFAIK, there is no way to escape a path separator character in Java classpath.
The related issue is tracked here: https://youtrack.jetbrains.com/issue/IDEABKL-7923/separators-in-classpath-not-escaped-run-fails
Have the same issue: when I create a project from scratch, even a simple Java project, whether I ask to generate sample code or not, even with autogenerated Main class with main method I've got the same error java.lang.ClassNotFoundException:
It doesn't even try to create out folder:

All old projects work perfectly until I delete out folder, then there's no way to get it back and such project stopped working as well. Returning out folder doesn't help
New files created in the old project do not work:
While old files keep working:
It all happened after I updated my version and nothing helped to fix it.
Please enable debug logs for compiler per https://intellij-support.jetbrains.com/hc/en-us/articles/207241085 and contact support at https://intellij-support.jetbrains.com/hc/requests/new with the sample project, the steps to reproduce and the logs via Help | Collect Logs and Diagnostic Data.
To make it work for me, I cleared the Compiler Output field, under File -> Project Structure.