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.

2
20 comments

Hello,

Could you please check that compiled class is present in "out " folder. Is it possible to attach full classpath from the output?

0

Hello Yaroslav,

thank you for response.

Here is a screenshot from the "out" folder:

Classpath is:

And in project structure:

 

0

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?

0

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:

"C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\jbr\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:61989,suspend=y,server=n -Xmx1024m -Xms256m -XX:MaxPermSize=250m -ea -Dplugin.classloader.debug=true -Djava.system.class.loader=com.intellij.util.lang.PathClassLoader -Didea.config.path=D:\data\IntelliJIdea23.1\system\plugins-sandbox\config -Didea.system.path=D:\data\IntelliJIdea23.1\system\plugins-sandbox\system -Didea.plugins.path=D:\data\IntelliJIdea23.1\system\plugins-sandbox\plugins -Didea.classpath.index.enabled=false -Djdk.module.illegalAccess.silent=true --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.ref=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.base/jdk.internal.vm=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/sun.nio.fs=ALL-UNNAMED --add-opens=java.base/sun.security.ssl=ALL-UNNAMED --add-opens=java.base/sun.security.util=ALL-UNNAMED --add-opens=java.desktop/java.awt=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd.peer=ALL-UNNAMED --add-opens=java.desktop/java.awt.event=ALL-UNNAMED --add-opens=java.desktop/java.awt.image=ALL-UNNAMED --add-opens=java.desktop/java.awt.peer=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED --add-opens=java.desktop/javax.swing=ALL-UNNAMED --add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED --add-opens=java.desktop/javax.swing.text.html=ALL-UNNAMED --add-opens=java.desktop/sun.awt.datatransfer=ALL-UNNAMED --add-opens=java.desktop/sun.awt.image=ALL-UNNAMED --add-opens=java.desktop/sun.awt.windows=ALL-UNNAMED --add-opens=java.desktop/sun.awt=ALL-UNNAMED --add-opens=java.desktop/sun.font=ALL-UNNAMED --add-opens=java.desktop/sun.java2d=ALL-UNNAMED --add-opens=java.desktop/sun.swing=ALL-UNNAMED --add-opens=jdk.attach/sun.tools.attach=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-opens=jdk.jdi/com.sun.tools.jdi=ALL-UNNAMED -Didea.required.plugins.id=DBN -Didea.platform.prefix=Idea -Didea.plugin.in.sandbox.mode=true -javaagent:D:\data\IntelliJIdea22.3\system\captureAgent\debugger-agent.jar=file:/C:/Users/CiocaD/AppData/Local/Temp/1/capture.props -Dfile.encoding=windows-1252 -classpath "D:\data\IntelliJIdea22.3\system\captureAgent\debugger-agent.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\log4j.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\jdom.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\trove4j.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\openapi.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\util.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\util_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\bootstrap.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\idea.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\3rd-party-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 23.1 CE\lib\jna.jar;C:\Program Files\JetBrains\IntelliJ IDEA 22.3\lib\idea_rt.jar" com.intellij.idea.Main

 

0

Actually my problem is with a DevKit plugin run configuration. I may be in the wrong thread here... 

0

Do you also have simple "Hello World" application?

0

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

2

It's very easy to fix

 

0

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?

0

PutUrHeartOndaTable A few things to try to fix it:

  • File | Invalidate Caches → check first three boxes → Invalidate & Restart
  • Temporarily disable ALL non-bundled plugins and restart the IDE: File | Settings | Plugins | ⚙ | Disable all downloaded plugins
  • Reset IDE settings to defaults (a backup copy will be created in the process): File | Manage IDE Settings | Restore Default Settings
0

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?

0

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?)

0

I actually fix that by moving the project from /Documents to /ProjectIdeas folder. Not sure why it is like that?

0

@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

0

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.

0

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.

0

To make it work for me, I cleared the Compiler Output field, under File -> Project Structure.
 

0

Please sign in to leave a comment.