False duplicate class error in NetBeans? This simple fix might take care of it…
With any program as large and complicated as NetBeans (an integrated development environment for Java), you’re sure to occasionally run into strange glitches like false errors.
You might be working on a moderately involved Java class in NetBeans, you might feel like you’re making some good progress, like you’re in the zone, when all of a sudden your editor window fills up with red error indicators.
Somehow, according to NetBeans, the class you’re working on suddenly became a duplicate class. And then it and its duplicate might as well not exist, causing a cascade of puzzling errors and warnings.
The existence of duplicate classes doesn’t make any sense, not when you consider that NetBeans requires Java classes be in files with the same name. For example, ClassA
must be in the file ClassA.java
, not any other file.
And with the directory-based project structure that NetBeans uses, a class must be in the folder named after the package it’s supposed to be in. Thus package01.ClassA
would have a relative path of package01/ClassA.java
.
So, even if you have classes with the same name in different packages, the operating system (Windows, Mac OS X, Linux, etc.) would not allow those source files to be in the same folder.
As strange as any particular glitch might seem, there’s always the possibility that it’s happened to someone else before, and that they left some kind of bug report somewhere. So fire up your favorite search engine.
This happened to me a couple of weeks ago, in a kata package, with the class EgyptianFractionViewer
. I was working on this with NetBeans 11.1 under macOS Mojave (I upgraded to Catalina last week).
People have been experiencing this false duplicate class problem at as far back as NetBeans 7.3.1, maybe earlier (I still use NetBeans 8.2 on my old Windows 8.2 computer, and I used it for the screenshots on this page).
As you might imagine, this problem is difficult to reproduce, as some of your search results for this problem will surely confirm. In hindsight, I should have taken a screenshot (I actually had two opportunities).
The solution I found online seemed a little too drastic. I decided to try something simpler first. I renamed the supposedly duplicate class EgyptianFractionViewera
. I just added an extra letter to its name.
You can rename the supposedly duplicated class nonsense if you prefer. Just choose a name that is unlikely to exist anywhere in your project, or your whole computer.
Be sure to do the renaming through the NetBeans Rename Class dialog (right-click on the class name and select Refactor > Rename… or press Ctrl-R or Control-R).
Hopefully this clears all the error indicators, leaving only yellow warning indicators, or maybe the green indicator.
If everything’s all good, you can now go ahead and rename the class back to what it was before.
I don’t guarantee that this will work every time you run into the false duplicate class error (and hopefully you won’t run into this problem more than once or twice in your many years of using NetBeans).
Nor do I guarantee that you will never run into this annoyance again. If my experience is any indication, if you run into this once, you will run into it again a few days later for the very same class. I just rename it each time.
If renaming to something else and renaming back to the original name doesn’t work, if NetBeans still says it’s a duplicate class, then you might need to take more drastic measures, like clearing the NetBeans cache.
Clearing the NetBeans cache is not difficult. It does require closing the program and deleting a file in a directory users should generally not touch, so if there’s an easier fix, you should try and see if that works first.