clankill3r
clankill3r

Reputation: 9543

Node cannot be resolved to a type

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

Answers (3)

geert3
geert3

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

Bradford Needham
Bradford Needham

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

Jon Bright
Jon Bright

Reputation: 13738

I've seen similar behaviour in the past and know of two possible reasons:

  • Your build path has somehow changed, leaving out your Node class, or the project providing it has compile errors, or similar. Given your description of the problem, this probably isn't relevant in your case.
  • Some Eclipse screwup. For me, this was always solved by doing a clean build of the project (possibly also any dependent projects) at which point things resolved themselves. You can do this by going to the Project menu and selecting Clean. This isn't a proper solution, but strikes me as quicker than your Node->DNode->Node workaround.

Upvotes: 2

Related Questions