Eclipse error: "The import XXX cannot be resolved"

476,295

Solution 1

I found the problem. It was the hibernate3.jar. I don't know why it was not well extracted from the .zip, maybe corrupt. A good way to check if jars are corrupt or not is navigating through their tree structure in "Project Explorer" in Eclipse: if you can't expand a jar node probably it's corrupt. I've seen that having corrupt packages it's frequent when you drag and drop them to the "Project Explorer". Maybe it's better to move and copy them in the OS environment! Thankyou all.

Solution 2

Try cleaning your project by going to the following menu item:

Project > Clean...

If that doesn't work, try removing the jars from the build path and adding them again.

Solution 3

Obviously there are cases where there is a valid issue however Eclipse throws this error for no good reason sometimes. This is still (in v 2020-09) an old (2010) Eclipse bug that can be resolved by making a negligible change to the project settings.

touch .classpath

solves the issue, or go to Project > Properties > Java Build Path > Order and Export > make a meaningless order change > Apply. Changing the order back does not regress to the problem.

Solution 4

Clean the project. And double-check the jars being really on the build path (with no errors). Also make sure there is nothing in the "Problems" view.

Solution 5

If is a Maven project, also click on Maven >> Update project... (F5)

Update Maven project

Share:
476,295

Related videos on Youtube

bluish
Author by

bluish

I'm here to learn and to improve help retrieval about IT. #SOreadytohelp You can contact me at rumoreblu snail gmail dot com ;)

Updated on December 05, 2021

Comments

  • bluish
    bluish over 2 years

    I'm trying to work with Hibernate in Eclipse. I'm creating a new simple project and I've downloaded a collegue project too, via CVS. Both don't work, while on my collegue's Eclipse do. The problem is that, for each import of an Hibernate class, Eclipse says:

    The import org.hibernate cannot be resolved
    

    But all Hibernate jars are in the build path, that is:

    antlr-2.7.6.jar
    cglib-2.2.jar
    commons-collections-3.1.jar
    dom4j-1.6.1.jar
    hibernate3.jar
    hibernate-jpa-2.0-api-1.0.0.Final.jar
    javassist-3.12.0.GA.jar
    jta-1.1.jar
    slf4j-api-1.6.1.jar
    
    • jmj
      jmj over 13 years
      how did you add these jar in your build path ?
    • bluish
      bluish over 13 years
      by Properties > Java Build Path > Libraries > Add external JARs
    • Ashwin Krishnamurthy
      Ashwin Krishnamurthy almost 9 years
    • Shanimal
      Shanimal over 8 years
      If it worked and they don't now, see @dogbane 's answer below...
    • schin chan
      schin chan almost 5 years
      check if there is any space in your import folder. if there is a space, remove the space and import projects onto eclipse.
    • Antoine
      Antoine over 4 years
      Clean Project did not work for me, but the good old trick of closing Eclipse and opening it again, did.
    • bluish
      bluish over 4 years
      @Antoine You could post your solution as an answer, as it could help someone to find it and other people to vote for it
  • bluish
    bluish over 13 years
    I've already tried this more then once: with individual jars, inside a library,... It seems an odd error!
  • Bozho
    Bozho over 13 years
    do you have anything in the Problems view?
  • bluish
    bluish over 13 years
    Only the problems I told and warnings of the type "The serializable class Azienda does not declare a static final serialVersionUID field of type long".
  • dogbane
    dogbane over 13 years
    might be a JRE issue. Try selecting an "alternate JRE" in the project properties.
  • Sabre Runner
    Sabre Runner over 10 years
    Clean also worked for me. What exactly happens (everything just went bonkers all of a sudden) and how does Clean help?
  • Zac
    Zac almost 10 years
    This solved the problem also for me; it is not an hibernate project, but a libGdx one, calling my java imports and not jars. Same wondering of @SabreRunner, and also: there is a bug somewhere in the environment tools?
  • Shanimal
    Shanimal over 8 years
    Worked for me too. Its super frustrating when the contextual help menu is finding your class and hand typing the same import above the ones marked "cannot be resolved" and no errors...
  • Timothy.Li
    Timothy.Li about 6 years
    This way does NOT help in my case. My Eclipse version is: Neon.3 Release (4.6.3)
  • Timothy.Li
    Timothy.Li about 6 years
    File > Properties Should be changed to Project > Properties.
  • Timothy.Li
    Timothy.Li about 6 years
    This way does NOT help in my case. My Eclipse version is: Neon.3 Release (4.6.3)
  • Timothy.Li
    Timothy.Li about 6 years
    In Neon.3 Release (4.6.3), there is NO Plug-in tools in the contextual menu by right-clicking on the project in Package explorer
  • kodmanyagha
    kodmanyagha almost 6 years
    If you import a maven project as "Java project" then this problem is occur. Convert project to maven project and update as you said then the problem is gone.
  • Anthony Hayward
    Anthony Hayward over 5 years
    For me, the JRE System Library had to be above the Maven Dependencies in the Build Path order&export tab.
  • user1133275
    user1133275 about 5 years
    This bug is now at least 9 years old.
  • Ashu
    Ashu over 4 years
    For me, install the latest eclipse with latest Oracle Java. Don't try to re-use the old eclipse workspaces.
  • Tullochgorum
    Tullochgorum over 4 years
    It's still there, Nov 2019. Sheesh.
  • Charlie
    Charlie over 4 years
    I removed the jar dependencies and added back again to have this problem fixed.
  • Skippy le Grand Gourou
    Skippy le Grand Gourou almost 4 years
    Just had the issue again, and it seems running mvn outside vim is not as efficient (that is, I tried several times without success while it worked immediately when ran through vim’s :Mvn).
  • bluish
    bluish over 3 years
    Hello Tom, I haven't read your answer, as it is very long. Please try to summarize it, or add a summary at the beginning of it, or at least highlight with bold characters the key concepts. Otherwise nobody will benefit from it. Hope you can understand, many thanks! ;)
  • Tom Rutchik
    Tom Rutchik over 3 years
    Oh and make sure the the package you're importing is a package that is being exported in the osgi headers
  • keesp
    keesp over 3 years
    Worked for me. After that changing it back to its original settings gave no problems. cleaning was not necessary (with Eclipse 2020-12)
  • tonyfarney
    tonyfarney about 3 years
    It worked for me, for a PHP project. Before I do the clean operation I've been add composer support and rebuild, but didn't work.
  • briancollins081
    briancollins081 over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • bluish
    bluish over 2 years
    @ItzToxic, this is a question about Eclipse and Java. You talk about vscode and Python... please delete your answer ;)
  • MD. RAKIB HASAN
    MD. RAKIB HASAN over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • GaidinD
    GaidinD over 2 years
    This fixed it for me. It was set to 15 for me. But the project was built to compliance level 1.8.