Error ClassNotFoundException in IntelliJ IDEA
Probably your project structure was src/main/java/start.java but when you added it to IntelliJ you have set src as a source folder, so IntelliJ put main.java as a package.
In Project Setting (Ctrl+Shift+Alt+S) → Modules → Sources tab set src/main/java as Source Folder. Then in your simple program change package to whatever you like (e.g. my.test). After that if IntelliJ reports any error in line with package quick fix with Alt+Enter should help
java.lang.ClassNotFoundException when running in IntelliJ IDEA
The error that you get occurs not on complilation, but when you try to run your application. It happens because Java was not able to find Table.class
file inside db
subdirectory of the project output directory (classpath).
It can happen for multiple reasons:
- wrong main class selected in the run/debug configuration
Table.java
is excluded from compilation (by accident or intentionally because it contained errors and you wanted to skip it while working on other code)- class not compiled because Build step is excluded from from Before launch steps in the Run/Debug configuration
- project is misconfigured and there is no Source root defined for the directory containing
db
subdirectory Table.java
has incorrectpackage
statement or is located/moved to a different package- project path contains a colon
:
on Mac/Linux or semicolon;
on Windows, it's used to separate the classpath and will render the classpath invalid. See this thread for details. Note that Finder on Mac may display colons in the path as slashes. - the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details.
- In project structure make sure you have the right Java version for compile.
- there is a known bug that sometimes a Java project created from the Command Line template doesn't work because
.idea/modules.xml
file references invalid module file nameduntitled104.iml
. Fix the module name manually or create a project from scratch and don't use a template. - on Windows "Beta: Use Unicode UTF-8 for worldwide language support" Region Setting is enabled. See IDEA-247837 for more details and workarounds.
- When IntelliJ IDEA is configured to store module dependencies in Eclipse format source root configuration is lost due to a known bug. Configure the module to use IntelliJ IDEA format dependencies as a workaround.
In a properly configured project and with the correct run/debug configuration everything works just fine:
intelliJ IDEA: java.lang.ClassNotFoundException
Exception in thread "main" java.lang.ClassNotFoundException: Main
In the run/debug configuration you have written wrong class name for the Main class field. The name of the class should be the class that has main
method to run.
This error could also happen if you didn't create or didn't select run/debug configuration for the class. To create it use Edit Configurations -> Add new under application tree item or from the editor press Alt+hift+F10 and choose your file to run. The configuration will be added automatically. To select current run/debug configuration use dropdown from the toolbar.
Usually I prefer to choose from menu Run -> Run, but as @Bajal mentioned in the comment, you can right-click on the file from the Project Structure and choose Run from the popup menu or press Ctrl+Shift+F10.
Why get java.lang.ClassNotFoundException when run as jar, but work well with IntelliJ IDEA
There is an option called Enable launch optimization
in IntelliJ IDEA run config, uncheck it and everything work as expected.
My project runs well from IntelliJ IDEA while encounters java.lang.ClassNotFoundException from CLI tool, how do I fix it?
You need to tell maven to include the dependencies. When IntelliJ runs it for you, it adds all the dependencies to the classpath, which is why it works from IntelliJ.
Try this
If you run mvn package and unzip the resulting jar with your updated pom.xml, you will see the dependencies are still not included.
The reason for this is that you need to put
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
under
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Not inside pluginManagement.
I tried it, it generates a jar called ...-with-dependencies and your java command then runs fine.
IntellijIDEA + Gradle + JavaFX: java.lang.ClassNotFoundException for one Run Configuration but not the other
All right.
It turns out that the widget for Build and Run is whitespace sensitive...
Which becomes obvious if one looks at the workspace.xml file
<option name="MAIN_CLASS_NAME" value="pack2.ImageViewExample " />
This is why XML should not be dismissed easily as config language.
A telling sign, if you know what to look for:
Well, that was easy.
Error report submitted to JetBrains.
Related Topics
Under What Conditions Is a Jsessionid Created
Can Enums Be Subclassed to Add New Elements
What Is the "Continue" Keyword and How Does It Work in Java
Scanner VS. Stringtokenizer VS. String.Split
Java: Parse Int Value from a Char
Hibernate Annotations - Which Is Better, Field or Property Access
Why Is Spring's Applicationcontext.Getbean Considered Bad
How to Set a Background Picture in JPAnel
Random Weighted Selection in Java
Java: Insert Multiple Rows into MySQL with Preparedstatement
Convert Java.Util.Date to Java.Time.Localdate
Does Use of Final Keyword in Java Improve the Performance
How Does a Arraylist's Contains() Method Evaluate Objects
What Is the Native Keyword in Java For
Replace Implicit Wait with Explicit Wait (Selenium Webdriver & Java)