How to eliminate ClassNotFoundException?

I try to run a Java application, but I get this error:

java.lang.ClassNotFoundException:

After the colon appears at the place where the class is missing. However, I know that this place does not exist, since the class is in another place. How can I update the path to this class? Does it have anything to do with the path to the class?

+49
java classpath class classnotfoundexception
Jul 01 '13 at 15:58
source share
13 answers

Your class path is broken (which is a very common problem in the Java world).

Depending on how you run your application, you need to review the -cp argument, your Class-Path entry in MANIFEST.MF, or your disk layout.

+20
Jul 01 '13 at 16:02
source share

The class path is a list of places to load classes.

These "locations" can be either directories or jar files.

For directories, the JVM will follow the expected pattern for loading the class. If I have a C: / myproject / classes directory in my class path and I try to load the com.mycompany.Foo class, it will look under the class directory for the com directory, then under this mycompany directory, and finally it will be look for a file named Foo.class in this directory.

In the second case, for jar files, it will look for a jar file for this class. A jar file is actually just a collection of directories like the ones above. If you unzip the jar file, you will get a bunch of directories and class files according to the template above.

So, the JVM traverses the class path from start to finish, looking for a class definition when trying to load a class definition. For example, in the classpath:

C: / MyProject / classes; C: /myproject/lib/stuff.jar; C: /myproject/lib/otherstuff.jar

First, the JVM will try to find the classes in the directory, then in stuff.jar and finally in otherstuff.jar .. p>

When you get a ClassNotFoundException, it means that the JVM went all the way to the classes and did not find the class you were trying to reference. The solution, as often in the Java world, is to test your class path.

You define the classpath on the command line by saying java -cp and then your classpath. In an IDE such as Eclipse, you will have a menu option to specify the class path.

+19
Jul 01 '13 at 16:20
source share

This is the best solution I've found so far.

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

  • HelloWorld (main class)
  • SupportClass
  • Utilclass

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

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

When calling Java, we specify the name of the application to be launched: org.mypackage.HelloWorld . However, we must also tell Java where to look for the files and directories that define our package. Therefore, to run the program, we must use the following command: enter image description here

NOTE. . You must execute the java command above regardless of your current location. But this does not apply to javac . For you can even go directly to the directory in which you have your .java files and directly execute javac ClassName.java .

+6
Oct 05 '15 at 6:44
source share

If you know the path to the class or jar containing the class, add it to your class path at the time it starts. You can use the classpath as indicated here:

on windows

 java -classpath .;yourjar.jar YourMainClass 

on UNIX / Linux

 java -classpath .:yourjar.jar YourMainClass 
+4
Jul 01 '13 at 16:00
source share

To add a class location to your class path through the command line, simply add -cp or -classpath and the class location at the time it starts. I.e.

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

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

+2
Jul 01 '13 at 16:05
source share

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

+2
Jan 19 '15 at 8:33
source share

I use maven for my project, and when I do mvn clean install and try to run the program, it throws an exception. So, I clean the project and run it again, and it works for me.

I am using the Eclipse IDE.

For:

Class not found Exception when running Junit test

Try running mvn clean test after it compiles all the test classes (will work for me).

+2
Apr 22 '15 at 19:48
source share

Add the full path of the jar file to CLASSPATH. On linux, use: export CLASSPATH=".:/full/path/to/file.jar:$CLASSPATH" . Another way of working (without editing CLASSPATH) is to unzip the jar in the current project folder.

They didn’t work for me:

1) Using the -cp option with the full path of the jar file.

2) Using -cp only with the name jar if it is in the current folder

3) Copy the flag to the current project folder

4) Copying jars to the standard location of java jars (/ usr / share / java)

This solution is reported for the class com.mysql.jdbc.Driver in mysql-connector-java.5 - *. jar running on Linux with OpenJDK version 1.7

+2
Dec 04 '15 at 9:05
source share

Go to the top and remove the import statement, if one exists, and re-import the class. But if it is not, make a clean assembly. Do you use Netbeans or Eclipse?

+1
Jul 01 '13 at 16:04 on
source share

I came across this and tried all other solutions. I did not have a .class file in my HTML folder, I only had a .java file. When I added the .class file, the program worked fine.

+1
Jan 13 '14 at 15:35
source share
  • This can happen if your classpath is incorrect.

  • We put the serializable class and the deserializable class under the same project name. You run a serializable class by creating a serializable object in a specific folder. Now you need deserialized data. In the meantime, if you change the name of the project, this will not work. First you need to run the serializable class, and then deserialize the file.

+1
Feb 10 '14 at 22:05
source share

If you are using maven try maven to update all projects and force snapshots. It will also clear and rebuild all classes. This solved my problem.

+1
Jan 11 '17 at 8:16
source share

Put all the code in a try block, and then catch the exception in the catch block

 try { // code } catch(ClassNotFoundException e1) { e1.getmessage(); } 
-one
Feb 23 '15 at 15:53
source share



All Articles