[java] Eclipse error: "The import XXX cannot be resolved"

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

This question is related to java eclipse import jar buildpath

The answer is


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.


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.


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.


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)

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

Update Maven project


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


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


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.


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


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


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


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.


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.


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.


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.


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.


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

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.


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


Or in you module-info.java, add:

requires org.hibernate;

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.


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.


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


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

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


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.


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


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.


  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


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.


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.


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


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


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.


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.


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.


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


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!


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


For me,

Project ---> Source ---> Format

solved the problem


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to import

Import functions from another js file. Javascript The difference between "require(x)" and "import x" pytest cannot import module while python can How to import an Excel file into SQL Server? When should I use curly braces for ES6 import? How to import a JSON file in ECMAScript 6? Python: Importing urllib.quote importing external ".txt" file in python beyond top level package error in relative import Reading tab-delimited file with Pandas - works on Windows, but not on Mac

Examples related to jar

Running JAR file on Windows 10 Add jars to a Spark Job - spark-submit Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Maven Error: Could not find or load main class Where can I download mysql jdbc jar from? Access restriction: The type 'Application' is not API (restriction on required library rt.jar) Create aar file in Android Studio Checking Maven Version Creating runnable JAR with Gradle

Examples related to buildpath

The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path getting JRE system library unbound error in build path Attach the Source in Eclipse of a jar Error in Eclipse: "The project cannot be built until build path errors are resolved" Setting the classpath in java using Eclipse IDE Android - R cannot be resolved to a variable Eclipse error: "The import XXX cannot be resolved" How to set the java.library.path from Eclipse