Eclipse 错误:“导入 XXX 无法解析”

发布于 2024-10-05 13:00:27 字数 454 浏览 9 评论 0原文

我正在尝试在 Eclipse 中使用 Hibernate。 我正在创建一个新的简单项目,并且我也通过 CVS 下载了一个同事项目。 两者都不起作用,而在我同事的 Eclipse 上却可以。 问题是,对于 Hibernate 类的每次导入,Eclipse 都会说:

The import org.hibernate cannot be resolved

但所有 Hibernate jar 都在构建路径中,即:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

揽清风入怀 2024-10-12 13:00:28

通过链接源解决。在资源管理器中右键单击项目,选择属性,选择 Java 构建路径,从选项卡中选择源,按“LinkSource”按钮。并指定要添加到项目的文件夹。

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.

强者自强 2024-10-12 13:00:28

我也无法导入。
我花了几个小时才弄清楚,当我尝试编译 1.8 时,我尝试使用 1.6 绑定的库/jar。
当我将我的项目也切换为使用 1.6 时,导入问题就消失了。
所有错误消息都导致错误的方向。
就在源代码中我发现了一些针对 1.6 版本的限制。
和:
例如.settings和.classpath(文件搜索)-> org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
可以在此类问题上给出提示。

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.

今天小雨转甜 2024-10-12 13:00:28

我遇到了同样的问题,我通过删除一个 jar 来解决它,该 jar 在我的 pom.xml 的两个不同依赖项中添加了两次。删除一个依赖项解决了这个问题。

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.

一直在等你来 2024-10-12 13:00:27

尝试通过转到以下菜单项来清理您的项目:

Project > Clean...

如果这不起作用,请尝试从构建路径中删除 jar 并再次添加它们。

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.

青春有你 2024-10-12 13:00:27

显然,在某些情况下存在有效的问题,但 Eclipse 有时会无缘无故地抛出此错误。这仍然是(在 v 2020-09 中)一个旧的 (2010) Eclipse 错误,可以通过对项目设置进行可忽略的更改来解决。

touch .classpath

解决问题,或转到“项目”>“属性> 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.

我三岁 2024-10-12 13:00:27

如果是Maven项目,还点击Maven >>> 更新项目... (F5)

更新 Maven 项目

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

Update Maven project

阪姬 2024-10-12 13:00:27

清理项目。并仔细检查 jar 是否确实位于构建路径上(没有错误)。还要确保“问题”视图中没有任何内容。

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.

近箐 2024-10-12 13:00:27

我发现了问题。这是 hibernate3.jar。我不知道为什么它没有从 .zip 中很好地提取出来,也许是损坏。检查 jar 是否损坏的一个好方法是在 Eclipse 的“Project Explorer”中浏览它们的树结构:如果无法展开 jar 节点,则它可能已损坏。我发现当您将包拖放到到“项目资源管理器”时,经常会出现损坏的包。也许最好在操作系统环境中移动和复制它们!谢谢大家。

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.

不及他 2024-10-12 13:00:27

这已经解决了我的问题。

1)清理项目Project->清理...

2) 右键单击​​项目 ->构建路径->配置BuildPath->库选项卡 ->
双击 JRE SYSTEM LIBRARY ->然后选择备用 JRE

3) 单击“保存”

4) 再次转到项目资源管理器中的项目,然后右键单击项目 ->构建路径->配置BuildPath->库选项卡 ->
双击 JRE SYSTEM LIBRARY ->这次选择“执行环境”

5)应用

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

拒绝两难 2024-10-12 13:00:27
  1. 清洁项目
    项目->干净...
  2. 右键单击项目 - >构建路径 - >配置构建路径 - >库选项卡 - >

双击 JRE SYSTEM LIBRARY - >然后选择备用 JRE

  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

执着的年纪 2024-10-12 13:00:27

我通过删除项目(不要在磁盘上删除)并再次导入来解决它。

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

峩卟喜欢 2024-10-12 13:00:27

我遇到了问题,类路径不知何故被破坏了。

因此,在 Package explorer > 中右键单击该项目;插件工具>更新类路径...为我做的

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

雪落纷纷 2024-10-12 13:00:27

我通过更新项目解决了这个问题:右键单击Maven项目,选择Maven ->更新项目->选择复选框强制更新快照/版本。

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.

疧_╮線 2024-10-12 13:00:27

尝试在项目的构建路径中添加 JRE 系统库。

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

挽袖吟 2024-10-12 13:00:27

对我来说,它有助于改变编译器合规性级别
由于未知原因,它被设置为 1.6,我将其更改为 1.8。

进入项目级别后,右键单击项目 >属性> Java 编译器,在 Eclipse 中单击菜单“窗口”>“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.

紫瑟鸿黎 2024-10-12 13:00:27

我尝试了上面所有的答案,但没有运气。就我而言,有一个生成的 build/classes 文件夹,其中包含一些附加的“.class”文件。我最终转到包资源管理器,右键单击该项目并选择“刷新”选项,这使得 build/classes 文件夹再次可用,从而解决了问题。

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.

就此别过 2024-10-12 13:00:27

就我而言,它是 Maven 存储库中的一个损坏的 jar。删除存储库中的 jar 文件并让 Maven 重新下载它们。

当我从命令行运行 mvn clean install 时,它运行良好,但 Eclipse 仍然无法编译代码。当我在 Eclipse 中运行 maven install 时,我看到 Maven 抱怨 jar 文件有问题。所以我删除了它并再次运行maven install。问题就消失了。

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.

心的憧憬 2024-10-12 13:00:27

为了下载 .m2 目录中不存在的 jar 文件,您应该为项目 pom.xml 运行 mvn clean install 命令。然后,您应该通过单击 Alt+F5 更新项目依赖项。这对我有用!

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!

满天都是小星星 2024-10-12 13:00:27

删除“module-info.java”确实解决了我的问题!

该文件是自动生成的,并且确实出现在我的层次结构中......

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

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

甜中书 2024-10-12 13:00:27

pom. xml

如果是 Maven 项目,则转到 pom.xml 并删除所有依赖 jar。保存起来。项目将自动构建。

然后再次添加依赖项 jar(您可以执行 Ctrl+Z)。保存起来。项目将自动构建,您将不会看到该错误。

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.

紫罗兰の梦幻 2024-10-12 13:00:27

我遇到了同样的问题,因为我添加了一个我创建的 jar,其中我设置了打包基目录而不是类的基目录。
因此,类(例如 java.util.List)必须作为 util.List 导入,尽管建议的导入是第一个导入。

检查引用的库下导入的jar,看看它们是否正确导入

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

呆萌少年 2024-10-12 13:00:27

我不明白这背后的原因,但这解决了我面临的同样的问题。在执行上述解决方案中提到的步骤之前,您可能需要执行这些步骤(清理所有项目并自动构建)。

右键项目->属性->项目方面 ->选择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

且行且努力 2024-10-12 13:00:27

每当您遇到此问题时,只需转到“项目”>“清理,然后选择清理所有项目。它应该得到解决,否则尝试删除这些罐子并再次添加它们。最后一件事是尝试重新启动 Eclipse。

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.

简单爱 2024-10-12 13:00:27

Eclipse IDE 的解决方案

在我的例子中,我通过更正以下两个可能损坏的文件之一来修复它:.classpath.project

要修复任何损坏的文件,您可以从另一个具有相关 .project 文件或 .classpath 文件且无错误的项目进行复制。但请确保不要添加不会使用的其他设置。

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.

时光匆匆的小流年 2024-10-12 13:00:27

删除 POM.XML 中的依赖项并下载 jar 并将其添加到构建路径中。

有时-> maven不会反映新的变化,这可能会导致这种问题

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

弥枳 2024-10-12 13:00:27

右键单击src->new package:重命名要导入的包,并使用包类名创建一个新类。
例如,如果导入 net.proteanit.sql.DbUtils;显示导入错误

,然后创建一个名为 net.proteanit.sql 的新包
并创建 DbUtils 类;

package net.proteanit.sql;
public class DbUtils {

}

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 {

}
意中人 2024-10-12 13:00:27

请尝试检查所有库是否就位。我有同样的问题。但我通过移动 lib 文件夹并在构建路径中再次添加所有 jar 解决了这个问题。

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.

忆依然 2024-10-12 13:00:27

我遇到了同样的问题。我下载了 jar 并将其添加到构建路径中,但我没有注意到扩展名是 .jar.zip。我再次将其转换为 .jar 并添加到构建路径中。

它解决了我的问题。这是一个非常愚蠢的错误,但我把它写在这里,以防它可以帮助别人。

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.

つ可否回来 2024-10-12 13:00:27

我使用 JPA 解决了这个问题来自 Maven 存储库的 JAR,将其添加到项目的构建路径中。

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

も星光 2024-10-12 13:00:27

如果您使用 Maven 并遇到此问题,请检查存储库服务器(例如 Nexus 服务器)是否存在工件。有时,他们可以更改工件的名称,而您尝试使用旧名称获取工件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文