162

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
bluish
  • 26,356
  • 27
  • 122
  • 180

47 Answers47

312

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 5
    might be a JRE issue. Try selecting an "alternate JRE" in the project properties. – dogbane Dec 01 '10 at 10:17
  • 12
    Clean also worked for me. What exactly happens (everything just went bonkers all of a sudden) and how does Clean help? – Sabre Runner Sep 22 '13 at 12:31
  • 1
    This solved the problem also for me; it is not an hibernate project, but a [libGdx](http://libgdx.badlogicgames.com/) one, calling my java imports and not jars. Same wondering of @SabreRunner, and also: there is a bug somewhere in the environment tools? – Zac Jul 19 '14 at 10:00
  • 3
    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... – Shanimal Jan 22 '16 at 19:20
  • 1
    This way does NOT help in my case. My Eclipse version is: Neon.3 Release (4.6.3) – Timothy.Li Mar 02 '18 at 02:14
  • For me, install the latest eclipse with latest Oracle Java. Don't try to re-use the old eclipse workspaces. – Ashu Oct 19 '19 at 07:18
  • I removed the jar dependencies and added back again to have this problem fixed. – Charlie Dec 27 '19 at 04:17
  • 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. – tonyfarney Feb 03 '21 at 21:21
38

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.

user1133275
  • 2,642
  • 27
  • 31
29

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

Update Maven project

albertoiNET
  • 1,280
  • 25
  • 34
  • 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. – kodmanyagha Jun 26 '18 at 22:53
22

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I've already tried this more then once: with individual jars, inside a library,... It seems an odd error! – bluish Dec 01 '10 at 09:41
  • 1
    do you have anything in the Problems view? – Bozho Dec 01 '10 at 09:50
  • 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". – bluish Dec 01 '10 at 09:55
17

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
9

I solved it by removing the project (do not delete on the disk) and importing it again.

bluish
  • 26,356
  • 27
  • 122
  • 180
SoldierShen
  • 91
  • 1
  • 1
9
  1. clean project Project - > clean...
  2. Right click on project - >BuildPath - >Configure BuildPath - >Libraries tab - >

Double click on JRE SYSTEM LIBRARY - >Then select alternate JRE

Balkrushna Patil
  • 418
  • 6
  • 12
9

This has solved my issue.

1) clean project Project -> clean...

2) Right click on project -> BuildPath -> Configure BuildPath -> Libraries tab -> Double click on JRE SYSTEM LIBRARY -> Then select alternate JRE

3) Click Save

4) Again go to your project in project explorer and right click to project -> BuildPath -> Configure BuildPath -> Libraries tab -> Double click on JRE SYSTEM LIBRARY -> This time select "Execution Environment"

5) Apply

Nisarg
  • 1,631
  • 6
  • 19
  • 31
Atul Waghmare
  • 91
  • 1
  • 1
  • Worked for me. After that changing it back to its original settings gave no problems. cleaning was not necessary (with Eclipse 2020-12) – keesp Jan 29 '21 at 09:01
7

I had the problem, that the classpath was broken somehow.

So right click on the project in Package explorer > Plug-in tools > Update classpath... did it for me

bluish
  • 26,356
  • 27
  • 122
  • 180
wuppi
  • 1,004
  • 10
  • 16
  • 1
    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 – Timothy.Li Mar 02 '18 at 02:31
7

I resolved this issue by updating the project: right click on the Maven project, select Maven -> Update Project -> select checkbox Force Updates of Snapshots/Releases.

bluish
  • 26,356
  • 27
  • 122
  • 180
5

Try adding JRE System Library in the build path of your project.

Gaurav Pant
  • 333
  • 1
  • 3
  • 5
5

With me it helped changing the compiler compliance level. For unknown reasons it was set to 1.6 and I changed it to 1.8.

Once at project level right click on project > Properties > Java Compiler, while in Eclipse click on menu Window > Preferences > Java > Compiler.

bluish
  • 26,356
  • 27
  • 122
  • 180
Alex P.
  • 3,073
  • 3
  • 22
  • 33
  • This fixed it for me. It was set to 15 for me. But the project was built to compliance level 1.8. – GaidinD Dec 17 '21 at 09:47
3

I tried all of the answers above but no luck. In my case, there is a generated build/classes folder with some additional ".class" files. I ended up going to the package explorer, right-clicking on the project and selecting the "Refresh" option and that made the build/classes folder available again resolving the issue.

egallardo
  • 1,234
  • 1
  • 15
  • 25
2

In my case it was a broken jar in the Maven repository. Delete jar files in repository and let Maven download them again.

When I ran mvn clean install from the command line, it ran fine, but Eclipse still could not compile the code. When I ran maven install in Eclipse then I saw that Maven complained about bad jar file. So I deleted it and ran maven install again. The problem was gone.

bluish
  • 26,356
  • 27
  • 122
  • 180
2

In order to download non-existing jar file in your .m2 directory, you should run mvn clean install command for your project pom.xml. Then you should update the project dependencies by clicking Alt+F5. This works for me!

bluish
  • 26,356
  • 27
  • 122
  • 180
ÖMER TAŞCI
  • 546
  • 5
  • 9
2

Removing the "module-info.java" did resolve the issue for me!

This file was automatically generated and did appear in my hierarchy...

val_t
  • 21
  • 1
2

pom.xml

If it's a Maven project, go to pom.xml and remove all the dependency jars. Save it. Project will automatically build.

Then add dependency jars again (you can do Ctrl+Z). Save it. Project will automatically build and you won't see that error.

bluish
  • 26,356
  • 27
  • 122
  • 180
sravan
  • 401
  • 1
  • 3
  • 8
1

I had the same problem because I added a jar I created, where I had set the packaging base directory other than the base directory of the classes. As a result the class e.g. java.util.List had to be imported as util.List although the suggested import was the first one.

Check the imported jars under referenced libraries to see that they are imported correctly

kon psych
  • 626
  • 1
  • 11
  • 26
1

I didn't understand the reasoning behind this but this solved the same problem I was facing. You may require these steps before executing steps mentioned in above solutions (Clean all projects and Build automatically).

right click project -> Properties -> Project Facets -> select Java -> Apply

avp
  • 2,892
  • 28
  • 34
1

Whenever you come across this problem just go to Project > Clean, then select Clean all projects. It should get resolved, otherwise try to delete those jars and add them again. Last thing would be to try restarting Eclipse.

bluish
  • 26,356
  • 27
  • 122
  • 180
1

Solution for Eclipse IDE

In my case I fixed it by correcting one of these two files that may be corrupt: .classpath or .project.

To repair any of the corrupted files, you can copy from another project that has both the .project file or .classpath file in question and is error-free. But be sure not to add additional settings that will not be used.

bluish
  • 26,356
  • 27
  • 122
  • 180
Tavo
  • 11
  • 1
1

Remove the dependency in your POM.XML and download the jar and add it in buildpath.

sometime -> maven will not reflect new changes and this might cause this kind of prob

1

right-click src->new package: Rename the package that you want to import and create a new class with the package class name.
E.g if import net.proteanit.sql.DbUtils; show import error

then create a new package with the name net.proteanit.sql and make class DbUtils;

package net.proteanit.sql;
public class DbUtils {

}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Please try and check whether all the libs are in place. I had the same issue. But I solved it by moving the lib folder and adding all the jars again in the build path.

bluish
  • 26,356
  • 27
  • 122
  • 180
0

I got the same problem. I downloaded the jar and added it to the build path, but I didn't notice that the extension was .jar.zip. I again converted it to .jar and added to the build path.

It solved my problem. It's a very silly mistake but I wrote it here in case it could help someone.

bluish
  • 26,356
  • 27
  • 122
  • 180
smali
  • 4,687
  • 7
  • 38
  • 60
0

I solved this problem using the JPA JAR from Maven repository, adding it on the build path of project.

bluish
  • 26,356
  • 27
  • 122
  • 180
0

If you are working with Maven and have this problem, check the repository server (for example nexus server), if the artifact is there. Sometimes, they can change the name of the artifact and you try to get the artifact with its old name.

drJava
  • 697
  • 2
  • 9
  • 24
0

Resolved by linking the source. From the explorer right click on the project, select properties, select Java Build Path, select Source from tabs, press button "LinkSource". And specifying the folder to add to the project.

0

I couldn't import as well. Took me some hours to figure out, that I tried to use a 1.6 bound library/jar, while I was trying to compile for 1.8. When I switched my project also to use 1.6, the import issue has gone. All error messages were leading into wrong directions. Just in the source I found some limitations directing to 1.6 version. And: For example the .settings and .classpath (File-Search) -> org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 can give a hint, on such issues.

sqller
  • 59
  • 5
0

I faced the same issue and I solved it by removing a jar which was added twice in two different dependencies on my pom.xml. Removing one of the dependency solved the issue.

Reema
  • 587
  • 3
  • 12
  • 37
0

For me, it seemed I was adding the wrong level of the library's folder. I downloaded the Jericho HTML Parser, unzipped it into eclipse-workspace/libraries/jericho-html-3.4/

Then when adding this as an external library folder, the trick was to navigate to that folder, highlight the "classes" folder and press Open. I had previously tried adding both the parent and various levels of subfolders and it didn't work.

This is pretty much explained here, it says

You can also add a class folder, a directory containing Java class files that are not in a JAR.

There are two lessons learnt here for me, apologies if these are off-topic:

  1. Read the instructions carefully - as in word-by-word!

  2. Do not give up - this it the first time I am adding a library, it took me around two hours to get it added.

pateksan
  • 160
  • 1
  • 12
0

[Code Igniter frame work] [Import library]

Try right-clicking on the project in a Project Explorer view, choose refresh. All error markers are gone surprisingly to my case.


I'm not good at Eclipse. I started using Android Studio to develop Android. But, it's annoying to see red error markers hovering all the time whilst the whole project still works fine. It happened to me when I use composer to import library like this:

require __DIR__ . '/../../vendor/autoload.php';

use \Firebase\JWT\JWT;
Nguyen Tan Dat
  • 3,780
  • 1
  • 23
  • 24
0

In my case, I imported a project which has been written on a classmate laptop, the problem is that he was using Java 11 and I was using Java 8, so the project had JDK 11 in it's build path, so I was unable even to import java classes (date, collection,...). So what I've done to solve the problem, is to delete the path of JDK11 and add the my JDK8 path.

Steps:

  1. Click on project on the menu bar then properties
  2. Choose the libraries tab
  3. Click on classpath then on add library from the sidebar
  4. Choose the corresponding library to add (remove older ones)
bluish
  • 26,356
  • 27
  • 122
  • 180
Mehdi Fracso
  • 448
  • 2
  • 16
0

For Eclim + Maven users :

  1. Open the .classpath file of your project : vim .classpath.
  2. Within vim, use :MvnRepo to initialize the Maven Eclim plugin. This will set M2_REPO. Note that this step *has to be performed while editing the .classpath file (hence step 1).
  3. Update the list of dependencies with :Mvn dependency:resolve.
  4. Update the .classpath with :Mvn eclipse:eclipse.
  5. Save and exit the .classpath:wq.
  6. Restarting eclim seems to help.

Note that steps 3 and 4 can be done outside vim, with mvn dependency:resolve and mvn eclipse:eclipse, respectively.

Since the plugin is mentioned as an Eclipse plugin in Eclim’s documentation, I assume this procedure may also work for Eclipse users.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
  • 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`). – Skippy le Grand Gourou Jul 07 '20 at 10:17
0

Or in you module-info.java, add:

requires org.hibernate;
0

I had a similar issue and this is the first thread that came up when I Googled for an answer.

What I had to do was in project > properties, go to Java Build Path > Libraries > Modulepath and change JRE System Library from (in my case) JavaSE-14 (unbound) to JavaSE-11 (Java SE 11.0.2 [11.0.2]).

I think the JavaSE-14 may not have been installed or something because it said (unbound) but the rest of the versions had a longer Java version name and number within the parentheses

I hope it helps somebody.

Kiran
  • 117
  • 3
  • 10
0

Add the jar files in class path instead of module path. Worked for me.

0

First Update Project in maven and Check for Jar/depdency in the required dependency in Class Path it Worked For me

0

I had a similar problem on an import statement not resolving. I tried many of the solutions offered in this discussion, but none of them solved my problem. I eventually figured out what was happening in my case so I'll share what I discovered.

If you add a jar to your build path and that jar is an OSGI bundle (a jar with a MANIFEST.MF file containing OSGI header statements), you cannot import any packages from that jar until you satisfy all Import-Packages requirements. Your only clue that this is happening is that you can't import packages from that bundle, even though it looks like the library is properly included in your project build path. Optional imported packages still need to be satisfied at compile time. Optional packages are allowed to be missing at runtime. Well that's for sure true if you're using the bundle in a OSGI framework, I'm not sure if that's true if you not using the bundle in a OSGI framework.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15
  • 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! ;) – bluish Dec 03 '20 at 07:52
  • Oh and make sure the the package you're importing is a package that is being exported in the osgi headers – Tom Rutchik Dec 04 '20 at 19:15
0

So reading through this answer I did not find a solution for Java developers working Visual Studio Code (VS code).

I was able to get this fixed in VSCode but entering crtl-shift-p and typing in clean. When I did that I ran the "Java: Clean Java Language Sever Workspace" command. This fixed my dependency issues.

Frederick Haug
  • 245
  • 2
  • 14
0

Also Try To Add main.java before your package name

Old Package Name: com.stackoverflow.help

Try: main.java.com.stackoverflow.help

0

My problem was vscode was running on an old Python installation

  1. Manually found and deleted the old installation

  2. Re-installed required imports

After this the false positive import errors were gone

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30370634) – briancollins081 Nov 18 '21 at 07:53
  • @ItzToxic, this is a question about Eclipse and Java. You talk about vscode and Python... please delete your answer ;) – bluish Nov 23 '21 at 13:37
0

In my case, I had 2k unresolved import errors, and the imports were legitimate, I could click on those import statements and have the respective class open up successfully, after trying several solutions, for example,

  1. Clean project,
  2. setting up alternative JDK,
  3. restarting eclipse,
  4. Ctrl+Shift+O organizing imports in eclipse,
  5. I even assumed it was a bug in eclipse,
  6. I could save the java source code it would get rid of the errors but upon rebuilding it, all the errors would come back.

none of them worked.

eventually, the root cause was, I was running in windows and the project folder had read-only access, and all I had to do was open eclipse with administrative access, and after clean build, all imports were resolved.

  • 1
    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](/help/how-to-answer). – MD. RAKIB HASAN Nov 30 '21 at 07:03
0

In my case I have upgraded the eclipse version from 2021-12 to 2022-09. My PC have 2 JDK 1.8 and 17 for which eclipse Java complier took the 17.

Moreover the Installed JRE was not using 1.8 JDK bin folder. Thus need to add JRE defining for it. This are set up things which might be missed and can be trouble some.

Solution:

  1. Check compiler in Preference-->JAVA-->Compiler
  2. Installed JRE should point to that JDK you going to use
  3. Check the respective project build path JDK/JRE settings

Image of Preferences menu in Eclipse

bluish
  • 26,356
  • 27
  • 122
  • 180
-1

For me,

Project ---> Source ---> Format

solved the problem

William Kinaan
  • 28,059
  • 20
  • 85
  • 118
-1

If you're developing plugins in eclipse and you're using a target platform (which was my case) you need to make sure you check the target's platform configuration from Window->Preferences->Plug-in Development->Target Platform and make sure your target platform is checked and configured. Hope this helps.

-2

Check If Your POM file is Updated with same Version in all Modules.

Shrutika
  • 11
  • 1