So I’m coding along and all of a sudden, Eclipse (3.4) can’t resolve classes. Classes that are in the same package as the class I’m editing. Classes that are fully qualified in the import statement.
Not good.
The last thing I had done was add a Spring @Autowired annotation. I saved the file and voila, everything goes to crap. I then spend a stupid amount of time trying to track down the cause. I took the changes out. I did a clean and rebuild and retest (everything passed; this was Eclipse-only). I added new classes to see if they broke (they did). I did a false modify (add space, remove space, save) to see if that broke said file (it did). It was a death spiral. No matter how I changed the code, the same problem was there.
Turns out, it wasn’t the annotation, or anything else in Spring, or any of my code, or any of my Eclipse plugins. It was a step before that killed me. I was doing a little cleanup and I created a temp directory at the top of my project folder so I could move some files there. It wasn’t nested in anything, other than the top level directory. What could go wrong?
Apparently, that makes Eclipse see red. Squiggly red.
The solution was simply to do a refresh (F5) on my project. Just as quickly as it started, the problem went away. That smells like a bug to me, but if it was I figure I’d see a lot more mentions in Google or the Eclipse bug tracker. I’m really hoping this helps someone save some time.
Update: If that doesn’t help you, see more solutions in the comments. Thanks to those who contributed them.
Keywords: false error, bogus error, eclipse bug
Thanks for your article. I had the exact same thing happen to me at the end of a Friday. I decided to hold off tackling the problem until Monday morning. I came and did a little searching and found your article.
You helped me save some time!
I am using the M2 plugin from Sonatype and found that I needed to do a Maven clean/install as well.
Glad I could help!
hi,
unfortunatelly, ur solution didnt help me.
i am having this problem for some time now. usu. it is fixed when i do a clean, and-build, refresh, but now for some reason it doesnt work anymore…
any suggestions are most welcome..
Hmm, not sure what to say. I imagine you’ve tried restarting and if that doesn’t work, exiting/relaunching (sometimes restart clear everything out). Possibly export/import might fix it if there’s something wrong with your .project. Clearly, I’m just guessing here. I’d try an Eclipse forum or your local JUG mailing list if you can’t solve this quickly.
thanks. i’ll keep on searching. its an annoying one…
I had the same problem and neither refreshing nor restarting worked. What worked though was deliberately making a syntax error in a type that couldn’t be resolved (despite being in the same package …) and saving the file.
It’s a weird bug in Eclipse that happens from time to time for no apparent reason. Most of the time a refresh does the trick though.
Its obvious that its a strange Eclipse bug.. Refreshing the project didn’t help me either. The solution for me was deleting the project from the workspace and importing the project again.
Thanks philip
This stupid eclipse bug is killing me.
Whenever I save a file I see those abominable red squiggly lines.
When I do project->clean they disappear all at once, but when I save the file again they reappear…
I just found when I uncheck “Project->Build automatically” the problem is solved.
That sounds rough, I always keep “build automatically” on. It’s a shame it’s causing errors instead of catching them for you.
This was killing me too. Project -> Clean worked for me but I wanted a more permanent solution.
Having worked for several hours just in Eclipse without a problem, I ran an Ant build externally and sure enough, the problem came back with my very next Save in Eclipse. I realized that my Ant task was deleting the class folders that Eclipse was compiling to. (Ant would empty ./build, then compile to ./build/web-inf/classes, and Eclipse to ./build/classes. My ‘clean’ task simply wiped out all of ./build. Then when Eclipse auto-compiled a class, it wouldn’t find any of the other required classes.)
My solution was to either point Eclipse to the EXACT same output folder as Ant (so everything would be in the right place after an Ant build) or point Eclipse at some other location that Ant doesn’t touch.
Hope this helps someone!
I use the Ant builder too.
I fixed the problem by changing the eclipse build folder to “build2″ so Ant and Eclipse don’t share their build folder.
Thanx Stephen
Thanks a lot Stephen! It seems to work for me.
Based on the comments here, I started checking how I could make sure that my Ant build wouldn’t interfere with my Eclipse build.
I found an option in Eclipse that clears up the problem (and is possibly more efficient than changing the 2 build system’s output folders).
Windows–>Preferences–>Java–>Compiler–>Building–>Output folder–>”Rebuild class files modified by others”. This exists in Eclipse 3.5; I’m not sure about earlier versions.
Checking this seems to have fixed my problems.
It simply worked! Thank you!
thanks a million, Davin Desnick!
Thank you David Resnick!
Many thanks for this blog contribution, Philip.
It helped me solve a vexatious problem on which I spent a lot of time trying to solve.
I finally saw the parallel between your problem and mine, and I finally could say: SOLVED.
Many many thanks
Thanks Phillip,
Deleteing the project and re-importing it into the workspace was what it needed.
Comment by David Resnick worked for me too, thanks.
It seems all class files aren’t the same? If Ant compiles the .class file to the (shared) build path and it’s up-to-date Eclipse determines by default that the file shouldn’t be overridden.
The ”Rebuild class files modified by others” setting tells Eclipse that if it needs the class file rebuilt (in a different format? or does it include extra properties) to just do it – rather than reporting problems about the class not existing.
Note, in 3.5 you could also have this setting on the project itself: Right-click the Project ->Properties->”Java Compiler”->Building->”Enable project specific settings”->”Rebuild class files modified by others”
Hey Guys,
THANKS A LOT for the comments regarding this “bug”. This problem was killing me !!!
Now my world is wonderful again
I did Project -> Clean and it went away
Thanks.
Thanks to David Desnick, it worked for me too.
Thanks to David Resnick for his comment.
Thanks to Philip Yurchuk for this article.
My workspace is red-squiggle-free again, and all is well.
Yup, Project -> Clean did it for me, just as well too as the laptop was heading for the window. You’ve saved my sanity and my laptop repair bill.
Huge thanks
Thanks David Desnick, it worked for me too.Did it for Eclipse 3.2.2..
Twice done succesfully. Disable “Build Automatically” and press File>Refresh (F5). Then you can enable “Build Automatically” and it will work.
Thanks. It worked for me.
Thanks! Your article helped!! For me, i was trying to refresh the whole project and still errors were present. Then i refreshed only the concerned packages where errors were appearing and this time ‘refresh’ worked…All errors gone now..But its lil strange
Thanks! I needed a “mvn clean install”. The problem started when an error crept into the install goal and it only partially finished.
Yeah, i did a mvn generate sources which sorta fixed the errors although it still showed errors in the class file below the java file. Then when i closed and re-opened the project, wa-la error gone. Gotta love those kind of IT problems :-LOL
Thank you Philip for the article!
And thank you David Resnick for the great tip!
[...] http://philip.yurchuk.com/2008/12/03/eclipse-cannot-be-resolved-to-a-type-error/ [...]
I have to add my name to the long list of those paying homage. This bug almost ruined my XMAS! I could almost rewrite that 12 days of xmas song with a new idea I had to attack a problem nearly as frustrating as japanese knott weed or a rodent infestation.
I found your blog. Thanks Phillip. The rebuild stuff is just 2nd nature to Java programmers so that had all been done. Likewise the point at a diff build folder didn’t feel right. A couple of the other solns didn’t feel good either.
David hit the nail on the head. Infestation gone with 2 shakes of a cats whiskers. Truly thank you. I would ocasionally build xternally with Maven just to keep that non-ide build on track. That killed it. Now I get to continue on my path – glad to have crossed yours.
Thank you both.
Refreshing the project didn’t help, but deleting the project from the workspace and re-importing the project did.
Thanks kamil!
Thanks to David Resnick which corrected my recurrent problem.
I am used to the “mvn clean install” command runned externally.
I’m just getting started with Eclipse, and am still in my petulant “man, I wish this was Visual Studio” phase. But this bug made me feel much more at home.
I’m deleting the contents of my “bin” folders from the command line, so that I can compress my project folders better. That causes this bug. I am able to fix it with the “cause syntax error, save, remove it, save again” technique, as well as with the “clean project” option.
Thanks for this post. Maybe in another two years they’ll fix it. Until then, keep up the good work!
to fix this you can either create your variable name using the package
p1.Person p = new p1.Person();
or
use the “set the import declarations for running code” button at the top of the window center right
[...] ?????Google??????????philip.yurchuk.com????????? This was killing me too. Project -> Clean worked for me but I wanted a more permanent solution. …… [...]
Cleaning the project fixed it for me. Cheers.
Hi all, i have some code in some different package, and when i try to use this package and its methods it is showing error as cannot be resolved to a type. i have tried the above mentioned techniques but could not solve the problem.
Please help me out asap.
This thread helped me alot, tried refresh, maven clean-install, but didn’t worked.
Finally i made some deliberate syntax error, saved file, undo syntax error, again saved files & all errors were gone
I have tried all the options in the blog and in the comment. Nothing helped. I am using maven and had other components. I wanted to debug them. But I have to close that component in order to start the project. I tried Maven > update project configuration. It solved my issue.
If you use a library that requires Java 6, while using Eclipse’s 1.5 level compiler settings, Eclipse does this.
Man, thank goodness for pdu’s entry from May 6 2009!
Same thing happened to me so I did what pdu said and introduced a syntax error intentionally (though I have a bunch of files/classes and had to introduce a bunch of errors, one for each file/error combo).
Then, and only then, could it get rid of the error. Man, I’d be using NetBeans if I could. Silly Eclipse….
Matt
Thanks David Resnick for the compiler setting solution.
Thanks Philip Y. for this article.
I tried all the options stated above nothing worked…One of my friends faced same issue and she suggested to remove all the jar files and add again and then refresh and build…This solved my issue…
Hello,
I did an uncheck auto build and David Resnick comment and then did a clean all. It worked!
Many thanks
Sameer
I had a similar problem; the difference was that I use ant in the command line to build, not enclipse (so build automatically is disabled).
As for others, suddenly it started showing “-cannot-be-resolved-to-a-type” errors.
After being stuck for hours; I tried the following (not sure why I did):
select an error in the “problems” tab and delete it. It worked.
So I selected all of them and simply deleted them.
Who would have thought!
(Btw its Eclipse’s unreliability the reason that I build everything from the command line)
This tiresome bug haunted me for abot TWO hours!!!!!
Luckily Closing project, Open Project, Project–>Clean did the trick!!