Eclipse error: "The import XXX cannot be resolved"
Asked Answered
E

46

166

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
Eulogy answered 1/12, 2010 at 9:30 Comment(7)
how did you add these jar in your build path ?Caoutchouc
by Properties > Java Build Path > Libraries > Add external JARsEulogy
https://mcmap.net/q/145482/-import-xxx-cannot-be-resolved-for-java-se-standard-classes-duplicateBun
If it worked and they don't now, see @dogbane 's answer below...Delenadeleon
check if there is any space in your import folder. if there is a space, remove the space and import projects onto eclipse.Mahound
Clean Project did not work for me, but the good old trick of closing Eclipse and opening it again, did.Tensible
@Tensible You could post your solution as an answer, as it could help someone to find it and other people to vote for itEulogy
E
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.

Eulogy answered 1/12, 2010 at 10:26 Comment(0)
L
313

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.

Leadsman answered 1/12, 2010 at 9:39 Comment(9)
might be a JRE issue. Try selecting an "alternate JRE" in the project properties.Leadsman
Clean also worked for me. What exactly happens (everything just went bonkers all of a sudden) and how does Clean help?Notochord
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?Pieter
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...Delenadeleon
This way does NOT help in my case. My Eclipse version is: Neon.3 Release (4.6.3)Melone
For me, install the latest eclipse with latest Oracle Java. Don't try to re-use the old eclipse workspaces.Atomy
I removed the jar dependencies and added back again to have this problem fixed.Leannaleanne
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.Fungous
dogbane where are jars in project?Gowk
N
39

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.

Namaqualand answered 15/7, 2016 at 20:55 Comment(6)
File > Properties Should be changed to Project > Properties.Melone
This way does NOT help in my case. My Eclipse version is: Neon.3 Release (4.6.3)Melone
For me, the JRE System Library had to be above the Maven Dependencies in the Build Path order&export tab.Keats
This bug is now at least 9 years old.Namaqualand
It's still there, Nov 2019. Sheesh.Craver
Just had the same issue, and updating .classpath fixed it. So, I guess still an issue in Eclipse 2022-03 (4.23.0)Stumpage
R
31

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

Update Maven project

Raspberry answered 14/12, 2016 at 7:56 Comment(1)
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.Floatstone
A
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.

Aureomycin answered 1/12, 2010 at 9:38 Comment(3)
I've already tried this more then once: with individual jars, inside a library,... It seems an odd error!Eulogy
do you have anything in the Problems view?Aureomycin
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".Eulogy
E
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.

Eulogy answered 1/12, 2010 at 10:26 Comment(0)
A
10
  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

Arteaga answered 22/1, 2018 at 12:18 Comment(1)
I changed SDK version, it worked excellent :)Busywork
M
10

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

Mcgannon answered 23/8, 2018 at 19:4 Comment(1)
Worked for me. After that changing it back to its original settings gave no problems. cleaning was not necessary (with Eclipse 2020-12)Troublous
A
9

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

Agraphia answered 28/5, 2015 at 7:46 Comment(0)
I
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

Isley answered 22/6, 2012 at 7:51 Comment(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 explorerMelone
S
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.

Spoilt answered 26/7, 2019 at 8:41 Comment(0)
E
5

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

Eldoree answered 19/6, 2013 at 7:39 Comment(0)
A
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.

Anam answered 1/8, 2017 at 12:46 Comment(1)
This fixed it for me. It was set to 15 for me. But the project was built to compliance level 1.8.Diagnose
L
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.

Laetitia answered 21/2, 2018 at 19:28 Comment(0)
H
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.

Hyp answered 24/8, 2018 at 9:28 Comment(0)
U
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!

Unbrace answered 15/1, 2019 at 10:46 Comment(0)
V
2

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

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

Vexillum answered 4/4, 2019 at 15:46 Comment(0)
T
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.

Tandratandy answered 28/8, 2019 at 6:39 Comment(0)
J
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

Jowers answered 25/10, 2012 at 10:4 Comment(0)
A
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

Attack answered 8/7, 2016 at 16:26 Comment(0)
A
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.

Adventurer answered 28/12, 2016 at 18:33 Comment(0)
M
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.

Moldboard answered 25/11, 2019 at 14:8 Comment(0)
M
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

Meyerhof answered 30/1, 2020 at 13:50 Comment(0)
S
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 {

}
Secure answered 5/12, 2021 at 4:9 Comment(0)
A
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.

Ayesha answered 11/3, 2014 at 9:35 Comment(0)
A
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.

Aleris answered 18/9, 2014 at 5:12 Comment(0)
H
0

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

Hemisphere answered 8/4, 2016 at 21:47 Comment(0)
H
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.

Hubing answered 10/5, 2016 at 10:15 Comment(0)
N
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.

Numbskull answered 19/7, 2017 at 14:47 Comment(0)
F
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.

Fistulous answered 27/10, 2018 at 12:13 Comment(0)
A
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.

Asepsis answered 20/12, 2018 at 18:20 Comment(0)
U
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.

Uninhibited answered 19/11, 2019 at 0:55 Comment(0)
U
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;
Unnecessarily answered 10/2, 2020 at 7:41 Comment(0)
C
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)
Clanton answered 11/2, 2020 at 20:55 Comment(0)
D
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.

Dialysis answered 23/6, 2020 at 15:59 Comment(1)
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).Dialysis
N
0

Or in you module-info.java, add:

requires org.hibernate;
Niobium answered 29/6, 2020 at 15:30 Comment(0)
A
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.

Anglice answered 2/7, 2020 at 19:50 Comment(0)
W
0

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

Wiliness answered 10/10, 2020 at 13:36 Comment(0)
A
0

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

Airily answered 23/10, 2020 at 3:34 Comment(0)
S
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.

Studdard answered 2/12, 2020 at 19:26 Comment(2)
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! ;)Eulogy
Oh and make sure the the package you're importing is a package that is being exported in the osgi headersStuddard
B
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.

Bathos answered 5/10, 2021 at 14:42 Comment(0)
F
0

Also Try To Add main.java before your package name

Old Package Name: com.stackoverflow.help

Try: main.java.com.stackoverflow.help

Fetal answered 12/10, 2021 at 7:36 Comment(0)
D
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.

Desdamona answered 30/11, 2021 at 5:43 Comment(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.Asafetida
S
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

Syverson answered 3/12, 2022 at 15:59 Comment(0)
W
-1

For me,

Project ---> Source ---> Format

solved the problem

Windlass answered 16/7, 2015 at 9:6 Comment(0)
N
-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.

Nenitanenney answered 19/10, 2016 at 8:47 Comment(0)
B
-2

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

Biting answered 20/2, 2017 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.