why I am getting Class not found exception

2020-03-07 07:51发布

问题:

I am using DeferredTextImpl Class and eclipse does not seems to complain about it but when I run my project I get run time exception .... Class not found exception for DeferredTextImpl ..

When I searched for the class file, I found it in rt.jar which should certainly be on the class path. I also checked the build path in project properties->Java build path and could see JRE System Library at the very bottom. when I expanded that. I could see rt.jar. which means it is on the class path, right ?

Why am I getting this error ?

回答1:

Check the version of your Xerces jar for your runtime versus you compile time classes. Make sure have a Xerces2 jar at runtime. I doubt the the class in the rt.jar is the same class your application is looking for.



回答2:

There is a difference between the build path in Eclipse and the classpath when you run your code. The build path is used to compile your code. The classpath is what your application has when it runs.

The build path is configured with Project -> Properties -> Java Build Path

The class path is configured with Run -> Run Configurations -> Classpath

In your case, you should also check (as others mentioned), if the JRE tab under Run Configurations points to the same JRE as in your build path.



回答3:

This class is part of a specific DOM implementation (Apache Xerces). It's not part of the public Java API, and if you do in fact find it in one JVM's runtime class library, this is no guarantee it'll be in others. If you need to explicitly work with Xerces, then you need to explicitly include the Xerces libraries on your runtime class path.



回答4:

Can you try running the code by explicitly adding rt.jar with -jar option while running the code? If this works then it means that the rt.jar in eclipse is not on classpath.



回答5:

Right click on project > java build path > Libraries remove JRE and add library pointing to JDK folder which in my case is C:\Program Files\Java\jdk1.7.0_55. But before that do the same thing with java installed JRE as well. Windows > java > installed JRE . Hope this will help