Eclipse “cannot be resolved to a type” error

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

Tags: ,

  1. Steve Daly’s avatar

    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.

  2. ng_agi’s avatar

    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..

  3. Philip Yurchuk’s avatar

    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.

  4. ng_agi’s avatar

    thanks. i’ll keep on searching. its an annoying one…

  5. pdu’s avatar

    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.

  6. kamil’s avatar

    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

  7. Tim’s avatar

    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…

  8. Tim’s avatar

    I just found when I uncheck “Project->Build automatically” the problem is solved.

  9. Philip Yurchuk’s avatar

    That sounds rough, I always keep “build automatically” on. It’s a shame it’s causing errors instead of catching them for you.

  10. Stephen’s avatar

    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!

  11. Jef Neefs’s avatar

    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

  12. sfish’s avatar

    Thanks a lot Stephen! It seems to work for me.

  13. David Resnick’s avatar

    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.

  14. jp’s avatar

    It simply worked! Thank you!

  15. ng_agi’s avatar

    thanks a million, Davin Desnick!

  16. Rob’s avatar

    Thank you David Resnick!

  17. MaltaCross’s avatar

    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

  18. Anthony’s avatar

    Thanks Phillip,
    Deleteing the project and re-importing it into the workspace was what it needed.

  19. Kevin Jansz’s avatar

    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”

  20. Jackson’s avatar

    Hey Guys,

    THANKS A LOT for the comments regarding this “bug”. This problem was killing me !!!

    Now my world is wonderful again ;)

  21. Raghav’s avatar

    I did Project -> Clean and it went away

    Thanks.

  22. Bayashi’s avatar

    Thanks to David Desnick, it worked for me too.

  23. Gene’s avatar

    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.

  24. niall’s avatar

    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

  25. Anirban Chowdhury’s avatar

    Thanks David Desnick, it worked for me too.Did it for Eclipse 3.2.2..