How do I resolve ClassNotFoundException?

2020-01-22 11:30发布

I am trying to run a Java application, but getting this error:

java.lang.ClassNotFoundException:

After the colon comes the location of the class that is missing. However, I know that that location does not exist since the class is located elsewhere. How can I update the path of that class? Does it have something to do with the class path?

19条回答
Fickle 薄情
2楼-- · 2020-01-22 11:44

Basic Generic Question - Simplest Generic Answer ;)

Given the information I will make the assumption that you might be trying a basic approach to coding, building/compiling and running a simple console app like "Hello World", using some simple text editor and some Command Shell.

This error occurs in the fallowing scenario:

..\SomePath>javac HelloWorld.java
..\SomePath>java HelloWorld.class

In other words, use:

..\SomePath>java HelloWorld

P.S. The adding the file extension .class produces the same mistake. Also be sure to have the Java's (JDK/JRE) bin folder in the operating system's Environment Variables's PATH.(Lookup for more details other posts on this) P.P.S Was I correct in my assumption/s?

查看更多
祖国的老花朵
3楼-- · 2020-01-22 11:45

To add the location of a class to your classpath via command line simply add -cp or -classpath and the location of the class while running it. I.E.

java -cp "c:/location/of/file" YourProgram

Or if you're running an IDE such as eclipse you can right click on the project -> build path -> configure build path and add the external JAR containing your class to the build path then it should work fine.

查看更多
叛逆
4楼-- · 2020-01-22 11:48

If you know the path of the class or the jar containing the class then add it to your classpath while running it. You can use the classpath as mentioned here:

on Windows

java -classpath .;yourjar.jar YourMainClass

on UNIX/Linux

java -classpath .:yourjar.jar YourMainClass
查看更多
干净又极端
5楼-- · 2020-01-22 11:49

This is the best solution I found so far.

Suppose we have a package called org.mypackage containing the classes:

  • HelloWorld (main class)
  • SupportClass
  • UtilClass

and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).

The file structure will look like this: enter image description here

When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we have to use the following command: enter image description here

NOTE: You have to execute the above java command no matter what your current location is. But this is not the case for javac. For compiling you can even directly go into the directory where you have your .java files and directly execute javac ClassName.java.

查看更多
时光不老,我们不散
6楼-- · 2020-01-22 11:51

Use ';' as the separator. If your environment variables are set correctly, you should see your settings. If your PATH and CLASSPATH is correct, windows should recognize those commands. You do NOT need to restart your computer when installing Java.

查看更多
再贱就再见
7楼-- · 2020-01-22 11:53
  1. It could happen if your classpath is not correct

  2. Let us posit a serializable class and deserializable class under same projectname. You run the serializable class, creating a serializable object in specific folder. Now you need the desearialized data. In the meantime, if you change the name of the project it will not work. You have to run the serializable class first and then deserialize the file.

查看更多
登录 后发表回答