Reputation: 9543
I'm working on a library and I would like to have the class Node. Most of the times it works fine but sometimes it gives the error Node cannot be resolved to a type
on every place where I use a node.
The strange thing is that if I rename the class to DNode, for example, and rename every Node to DNode and then change everything back again to just Node, it's like the way it was before but the errors saying Node cannot be resolved to a type
are gone (until a certain time).
Compiling is also no problem btw, it seems to be an Eclipse bug or something.
I know there is this Node class, but I don't import it so I can't see why that would form a problem.
I hope someone can help.
Upvotes: 3
Views: 16811
Reputation: 7341
I've had this problem when upgrading a project to Java9 or higher. Java9 introduces the concept of modules, and having more than one module "provide" a package is an issue that seems to cause this type of error messages in Eclipse. The error message ... cannot be resolved to a type
is misleading but I interpret it as ... cannot be resolved to a *unique* type
, then it makes sense.
Now to solve the issue you need to find out what jars on your classpath contribute what duplicate packages. There is an easy way to do that: use jdeps
:
For instance I used this:
> jdeps -verbose -cp lib\* src
Warning: split package: javax.xml.stream jrt:/java.xml lib\jsr173_1.0_api.jar
Warning: split package: javax.xml.stream.events jrt:/java.xml lib\jsr173_1.0_api.jar
Warning: split package: javax.xml.stream.util jrt:/java.xml lib\jsr173_1.0_api.jar
Warning: split package: org.w3c.dom jrt:/java.xml lib\jaxen-1.1-beta-7.jar
As can be seen from the above, Java standardly provides the above XML related modules, so removing the listed jars from classpath solved the problem. In some other cases you need to add only the "API" jars, not the "IMPL" jars. For instance add javax.servlet-api.jar
instead of j2ee.jar
Upvotes: 0
Reputation: 681
It seems several people described this problem (and a solution) a few years ago. For example, this describes the problem and found that doing a Refresh in Eclipse fixed it. In his case, he caused the problem by creating a top-level folder in his Eclipse Project directory - outside of Eclipse. It sounds like an Eclipse bug that's been there for a few years.
Upvotes: 1
Reputation: 13738
I've seen similar behaviour in the past and know of two possible reasons:
Project
menu and selecting Clean
. This isn't a proper solution, but strikes me as quicker than your Node
->DNode
->Node
workaround.Upvotes: 2