Error: Could not find or load main class Main in Intellij IDE
已回答
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.
请先登录再写评论。
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.
Same here. No longer working.
Do you also have simple "Hello World" application?
@...
After having wasted +8 hours dealing with this today I don't feel I have any time or energy to repeat this tomorrow. It might not be straightforward to reproduce as structure might be part of it as Afrina mentioned here:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/5240746520466--Could-not-find-or-load-main-class-when-using-template-after-installing-second-JDK?page=1
I reported the issue here:
https://youtrack.jetbrains.com/issue/IDEA-318005/Error-Could-not-find-or-load-main-class-com.intellij.idea.Main-Caused-by-java.lang.ClassNotFoundException-com.intellij.idea.Main
And commented on many similar tickets, whereby this appears to be known:
https://youtrack.jetbrains.com/issue/IDEA-314787
Suggested patch but has not tried or read it:
https://dkimitsa.github.io/2023/03/08/devkit-fixes-idea2023-1/
I do not use gradle or kotlin for plugin development, and I have my reasons for that, which are valid and good.
If you want to see what is happening we can scheduele a timeslot and I can screenshare, other than that, my plugin development happened today, change by change, rebuilding, zipping manually, getting into issues, manually installing zip file while trying to get this to work as it used to.
Can not put more time into this at this time. Hope you fix this issue til next time.
I doubt any deployment relying on the old devkit actually works currently as the runtime args seems to be completely off. See the ticket I filed.
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.