Java Eclipse 错误:“无法解析导入 XXX”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4322893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse error: "The import XXX cannot be resolved"
提问by bluish
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:
我正在尝试在 Eclipse 中使用 Hibernate。我正在创建一个新的简单项目,并且我也通过 CVS 下载了一个同事项目。两者都不起作用,而在我同事的 Eclipse 上。问题是,对于 Hibernate 类的每次导入,Eclipse 都会说:
The import org.hibernate cannot be resolved
But all Hibernate jars are in the build path, that is:
但是所有 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
采纳答案by bluish
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 dropthem to the "Project Explorer". Maybe it's better to move and copy them in the OS environment! Thankyou all.
我发现了问题。它是 hibernate3.jar。我不知道为什么它没有从 .zip 中很好地提取出来,可能是损坏的。检查 jar 是否损坏的一个好方法是在 Eclipse 的“项目资源管理器”中浏览它们的树结构:如果您无法展开 jar 节点,则它可能已损坏。我已经看到,当您将它们拖放到“项目资源管理器”时,经常会出现损坏的包。也许在操作系统环境中移动和复制它们会更好!谢谢你们。
回答by Bozho
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.
清理项目。并仔细检查 jars 是否真的在构建路径上(没有错误)。还要确保“问题”视图中没有任何内容。
回答by dogbane
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.
如果这不起作用,请尝试从构建路径中删除 jar 并再次添加它们。
回答by wuppi
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
所以在 Package explorer > Plug-in tools > Update classpath... 中的项目上右键单击...为我做了
回答by kon psych
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.
我遇到了同样的问题,因为我添加了一个我创建的 jar,我在其中设置了除类的基目录之外的打包基目录。因此,类 egjava.util.List
必须被导入,就像util.List
建议的导入是第一个一样。
Check the imported jars under referenced libraries to see that they are imported correctly
检查引用库下导入的jar包是否正确导入
回答by Gaurav Pant
Try adding JRE System Library in the build path of your project.
尝试在项目的构建路径中添加 JRE 系统库。
回答by Ravikumar
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.
请尝试检查所有库是否都到位。我遇到过同样的问题。但是我通过移动 lib 文件夹并在构建路径中再次添加所有 jar 来解决它。
回答by Ali786
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.
我遇到了同样的问题。我下载了 jar 并将其添加到构建路径中,但我没有注意到扩展名是 .jar.zip。我再次将其转换为 .jar 并添加到构建路径中。
It solved my problem. It's a very silly mistake but I wrote it here in case it could help someone.
它解决了我的问题。这是一个非常愚蠢的错误,但我在这里写了它,以防它可以帮助某人。
回答by SoldierShen
I solved it by removing the project (do not delete on the disk) and importing it again.
我通过删除项目(不要在磁盘上删除)并再次导入来解决它。
回答by William Kinaan
For me,
为了我,
Project ---> Source ---> Format
项目 ---> 源 ---> 格式
solved the problem
解决了问题