简体   繁体   中英

IntelliJ: How to resolve java.lang.ClassNotFoundException: main.java.org.***.Main

I'm using IntelliJ and the path to the directory that includes my source code is: MyProjectFolder/src/main/java/org/***/

This directory includes a java file named Main.java, and in Run/Debug Configurations I have the path "/home/matan/Projects/java_phone_book/src/main" in Working directory field.

Still, when I run the program by IntelliJ, I get the error:

Exception in thread "main" java.lang.ClassNotFoundException: main.java.org.***.Main

What can resolve this problem?

Your class' fully-qualified name is not main.java.org.***.Main . That is why the class main.java.org.***.Main can not be found.

If you are following a currently popular project layout, then 'main' and java are not part of the package name for your class. Try org.***.Main instead. (although asterisks are not a valid package name, so what you posted can't actually be it)

Otherwise, you'll need to go back to review your CLASSPATH, your package declarations, and your project layout and ensure they all agree with each other.

Go to the menu File > Project Structure. Click the "Modules" section, then click your module name, then click the "Sources" tab. One of the folders shown should be marked as a "Source Folder" already. I will call the name of this folder "S". Make sure Main.java is somewhere in one of the sub-folders of S.

Now, find the full directory path of Main.java. This should be something like S/<some_path>/Main.java . If the beginning of <some_path> is "java" then you should rename that directory to something else. Finally, in Main.java, change the first line of the code so that it says package <some_path>; Replace <some_path> with the one you found in the last step. Replace the slashes with dots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM