joepetrakovich
joepetrakovich

Reputation: 1366

How do you repair the ClassNotFoundException after you have changed the name of a class while building it

I changed the name of one of my java classes and now I am getting a classNotFoundException after cleaning and rebuilding in eclipse. I think this is a trivial and that I have forgotten the once known solution! Any Ideas?

Edit: I ended up doing the slowest solution, just creating a new project and putting all code in it...

Upvotes: 0

Views: 3939

Answers (3)

Yao Li
Yao Li

Reputation: 2266

In case someone has similar error with me and have no luck after clean project, it has the renamed old class bean in a specific xml file (like webapp/WEB-INF/spring/grape-servelet.xml in my case), you should manually change it to the new class name and then it will resolve the issue.

Upvotes: 0

Tim Bender
Tim Bender

Reputation: 20442

Did you use eclipse's refactoring? Or do you need to go organize the imports of some files still?

Edit based on OP feedback:

Eclipse often does a lot of stuff for Java developers in the background. Among these is including imports to a class reference in another class file. If you renamed a class (and it is the top level class for which the file is named) the compiler will first complain of this. Suppose you rename both the class and the file, now any other Java file which referenced the previous class name will no longer compile. In order to get things working again you will need to go through and fix the import declarations as well as code references in any file which indicates it does not compile. Eclipse also happens to use incremental compiling as well as maintain a memory model of the projects being developed. After verifying that there are in fact no errors, if using the clean utility doesn't work, try closing eclipse, deleting the contents of the bin directory manually, and then restarting eclipse. This should cause it to perform a full build of the project.

In the future, to avoid this problem entirely, be sure to always use the refactor utility built into eclipse. Refactoring is as easy as selecting a file, class, method, or variable name and pressing alt+shift+r and filling in the new desired name. The advantage to refactoring is that it will automatically chase down all the code references (that is has access to) for the developer.

Upvotes: 3

bluehallu
bluehallu

Reputation: 10275

Use refactor next time. By now, try to remove the already built classes and to compile again.

Upvotes: 1

Related Questions