“Error: Could not find or load main class …”

2019-08-09 14:13发布

import java.util.Scanner;
import java.io.*;
import java.io.FileNotFoundException;

public class bookreader {
    public static void main(String[] args) throws FileNotFoundException {
        //...
    }
}

When I run this code normally it spits out:

Exception in thread "main" java.lang.ClassNotFoundException: bookreader
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

When I debug the code it spits out:

Error: Could not find or load main class bookreader

I'm pretty sure both errors are the same, but I don't know how to fix either of them. Other answer point towards it running as a wrong file type, but the name of the file is bookreader.java which should run it in Java like needed in Intellij.

标签: java
2条回答
霸刀☆藐视天下
2楼-- · 2019-08-09 14:17

Make sure you are calling the class/Running the program like this :

"java bookreader" and NOT like "java bookreader.class"

查看更多
甜甜的少女心
3楼-- · 2019-08-09 14:33

Try the down-to-up method: Code a simple "Hello world" class, with no packages and no imports, and run it from the IDE. If it compiles well but does not run, either the project is not OK, or either the IDE is not OK.

Instead, if it runs, add more logic to this dummy class to make it resemble your initial class, repeating the test after every edit. When it fails, you'll know what was the last edit.

查看更多
登录 后发表回答