Eclipse error: "The import XXX cannot be resolved"

JavaEclipseImportJarBuildpath

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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 2 - Java

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 3 - Java

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 4 - Java

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

Update Maven project

Solution 5 - Java

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 6 - Java

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

Solution 7 - Java

  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

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

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.

Solution 11 - Java

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

Solution 12 - Java

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.

Solution 13 - Java

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.

Solution 14 - Java

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.

Solution 15 - Java

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!

Solution 16 - Java

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

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

Solution 17 - Java

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

Solution 18 - Java

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

Solution 19 - Java

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.

Solution 20 - Java

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.

Solution 21 - Java

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.

Solution 22 - Java

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

Solution 23 - Java

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.

Solution 24 - Java

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.

Solution 25 - Java

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.

Solution 26 - Java

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.

Solution 27 - Java

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.

Solution 28 - Java

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.

Solution 29 - Java

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.

Solution 30 - Java

[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;

Solution 31 - Java

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)

Solution 32 - Java

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.

Solution 33 - Java

Or in you module-info.java, add:

requires org.hibernate;

Solution 34 - Java

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.

Solution 35 - Java

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

Solution 36 - Java

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.

Solution 37 - Java

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.

Solution 38 - Java

Also Try To Add main.java before your package name

Old Package Name: com.stackoverflow.help

Try: main.java.com.stackoverflow.help

Solution 39 - Java

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

Solution 40 - Java

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.

Solution 41 - Java

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 {

}

Solution 42 - Java

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

Solution 43 - Java

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.

Solution 44 - Java

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

Solution 45 - Java

For me,

>Project ---> Source ---> Format

solved the problem

Solution 46 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbluishView Question on Stackoverflow
Solution 1 - JavadogbaneView Answer on Stackoverflow
Solution 2 - Javauser1133275View Answer on Stackoverflow
Solution 3 - JavaBozhoView Answer on Stackoverflow
Solution 4 - JavaalbertoiNETView Answer on Stackoverflow
Solution 5 - JavabluishView Answer on Stackoverflow
Solution 6 - JavaSoldierShen View Answer on Stackoverflow
Solution 7 - JavaBalkrushna PatilView Answer on Stackoverflow
Solution 8 - JavaAtul WaghmareView Answer on Stackoverflow
Solution 9 - JavawuppiView Answer on Stackoverflow
Solution 10 - JavaKalahasthi SatyanarayanaView Answer on Stackoverflow
Solution 11 - JavaGaurav PantView Answer on Stackoverflow
Solution 12 - JavaAlex P.View Answer on Stackoverflow
Solution 13 - JavaegallardoView Answer on Stackoverflow
Solution 14 - JavaEvgeny ShmulevView Answer on Stackoverflow
Solution 15 - JavaÖMER TAŞCIView Answer on Stackoverflow
Solution 16 - Javaval_tView Answer on Stackoverflow
Solution 17 - Javakon psychView Answer on Stackoverflow
Solution 18 - JavaavpView Answer on Stackoverflow
Solution 19 - JavaSrinivas ThammanaboinaView Answer on Stackoverflow
Solution 20 - JavasravanView Answer on Stackoverflow
Solution 21 - JavaTavoView Answer on Stackoverflow
Solution 22 - JavaRajagopal KView Answer on Stackoverflow
Solution 23 - JavaRavikumarView Answer on Stackoverflow
Solution 24 - JavasmaliView Answer on Stackoverflow
Solution 25 - JavadrJavaView Answer on Stackoverflow
Solution 26 - JavalivelovelaughView Answer on Stackoverflow
Solution 27 - JavasqllerView Answer on Stackoverflow
Solution 28 - JavaReemaView Answer on Stackoverflow
Solution 29 - JavapateksanView Answer on Stackoverflow
Solution 30 - JavaNguyen Tan DatView Answer on Stackoverflow
Solution 31 - JavaMehdi FracsoView Answer on Stackoverflow
Solution 32 - JavaSkippy le Grand GourouView Answer on Stackoverflow
Solution 33 - JavaGrégori Fernandes de LimaView Answer on Stackoverflow
Solution 34 - JavaKiranView Answer on Stackoverflow
Solution 35 - JavaEscanorVegetaView Answer on Stackoverflow
Solution 36 - JavaTom RutchikView Answer on Stackoverflow
Solution 37 - JavaFrederick HaugView Answer on Stackoverflow
Solution 38 - JavaVaibhav NalawadeView Answer on Stackoverflow
Solution 39 - JavaItzToxicView Answer on Stackoverflow
Solution 40 - JavaArun KumaraswamyView Answer on Stackoverflow
Solution 41 - JavaPrachi NanavatiView Answer on Stackoverflow
Solution 42 - JavaLeandro BoniniView Answer on Stackoverflow
Solution 43 - JavaStoyan ChervenkovView Answer on Stackoverflow
Solution 44 - JavaArijit DasView Answer on Stackoverflow
Solution 45 - JavaWilliam KinaanView Answer on Stackoverflow
Solution 46 - JavaShrutikaView Answer on Stackoverflow