Java 在 IntelliJ 中找不到类文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2199711/
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
Class file not found in IntelliJ
提问by homaxto
In IntelliJ I get this weird error when I try to compile.
在 IntelliJ 中,当我尝试编译时出现这个奇怪的错误。
Cannot find annotation method 'cascade()' in type 'javax.persistence.ManyToOne': class file for javax.persistence.ManyToOne not found
I know, it seems pretty obvious what the problem is, but having spent too much time on this problem I now turn my trust to you!
我知道,问题出在哪里似乎很明显,但是在这个问题上花费了太多时间,我现在将信任转向您!
I use ideaCommunity-9, where I have created a global library for JBoss, which contains all JBoss jars including ejb3-persistence.jar. My module includes this library, and I have moved it to the top of dependencies.
No otherwhere in classpath I have a javax.persistence.ManyToOne class, and it also opens up in IntelliJ without problems.
If I open the file where I get the compile error, IntelliJ shows no problems and also navigates to the class file on pressing Ctrl+B.
The code is also build with ant and here it works fine.
我使用ideaCommunity-9,我在其中为JBoss 创建了一个全局库,其中包含所有JBoss jar,包括ejb3-persistence.jar。我的模块包含这个库,我已经将它移到依赖项的顶部。在类路径中没有其他地方我有一个 javax.persistence.ManyToOne 类,它也在 IntelliJ 中打开没有问题。
如果我打开出现编译错误的文件,IntelliJ 不会显示任何问题,并且还会在按 Ctrl+B 时导航到类文件。
代码也是用 ant 构建的,在这里它工作正常。
Besides the above error I get six others along the same path (name in Table, etc.) - all related to JPA. I also get a message saying "Please file a bug to JDC yada yada yada."
除了上述错误之外,我还得到了其他六个相同路径(表中的名称等)——所有这些都与 JPA 相关。我还收到一条消息,说“请向 JDC yada yada yada 提交错误。”
采纳答案by Roman
Try to tick a checkbox "export" in your dependencies.
尝试在您的依赖项中勾选“导出”复选框。
I mean: "File" menu -> Project Structure -> Modules -> Dependencies, and there you can see libs included to the module. You should also see a checkbox near each lib in column 'Export'.
我的意思是:“文件”菜单 -> 项目结构 -> 模块 -> 依赖项,在那里你可以看到模块中包含的库。您还应该在“导出”列中的每个库附近看到一个复选框。
回答by VonC
Isn't that similar to this SO question?
这不是类似于this SO question吗?
The solution there was to add all JBoss jar (which you did), but also all persistent-related libraries (like Hibernate)
那里的解决方案是添加所有 JBoss jar(您所做的),以及所有与持久相关的库(如 Hibernate)
See this JBoss project configurationfor example:
例如,请参阅此JBoss 项目配置:
alt text http://jdick.co.uk/blog/wp-content/uploads/2008/12/projectstructure1.jpg
替代文字 http://jdick.co.uk/blog/wp-content/uploads/2008/12/projectstructure1.jpg
In short, ejb3-persistence.jar
might not be the problem here, just a symptom of otherelements missing.
Or, if it is the problem, it may be related to the fact that it is not deployed correctly (for the WebApp configuration).
简而言之,ejb3-persistence.jar
可能不是这里的问题,只是缺少其他元素的症状。
或者,如果是问题,则可能与未正确部署有关(对于WebApp 配置)。
回答by Yuval F
Here's an idea:
Maybe you have multiple versions of your jar file.
Search your folder hierarchy for the file ejb3-persistence.jar
,
using a file management tool such as Total Commander.
Multiple appearances of the file in your classpath may confuse IntelliJ Idea
into not recognizing the proper class in the proper jar.
这是一个想法:也许您有多个版本的 jar 文件。ejb3-persistence.jar
使用文件管理工具(例如 Total Commander)搜索文件的文件夹层次结构。类路径中文件的多次出现可能会使 IntelliJ Idea 混淆,无法识别正确 jar 中的正确类。
回答by rogerdpack
had a similar problem ("cannot find class spring...DefaultResourceLoader even though it wasin my maven dependencies) and no amount of tweaking of revalidating caches seemed to help. Blowing away my $HOME/.Idea* paths and (basically) starting from scratch fixed it for me. At least for now...
也有类似的问题(“无法找到班春... DefaultResourceLoader即使它是在我的Maven依赖),并没有重新验证缓存的调整量似乎帮助。吹走我的$ HOME / .Idea *路径和(基本)开始从头开始为我修复它。至少现在......
回答by Buddha
I'm sure, it is too late to answer, but I had to follow a different approach for this error to resolve this issue.
我敢肯定,现在回答为时已晚,但我必须针对此错误采用不同的方法来解决此问题。
I had to create this as global library and add that library to the module, then only the error is resolved.
我必须将其创建为全局库并将该库添加到模块中,然后才解决了错误。
回答by Nick Grealy
I was getting the class not found
error message when trying to execute a .groovy
test class, which referenced a .groovy
file (located in the root of the source directory of a submodule - i.e. no packages).
我class not found
在尝试执行一个.groovy
测试类时收到错误消息,该类引用了一个.groovy
文件(位于子模块源目录的根目录中 - 即没有包)。
Moving the source file .groovy
into a package, fixed the issue for me.
将源文件移动.groovy
到一个包中,为我解决了这个问题。
Move:
移动:
src/main/groovy/RunMe.groovy
to
到
src/main/groovy/somePackage/RunMe.groovy
回答by developer747
In intelliJ I got this problem when I built (ctrl+shift+F9) at the file level.
在 intelliJ 中,我在文件级别构建 (ctrl+shift+F9) 时遇到了这个问题。
The reason is, I had changes in other files in the same maven project, that were not getting picked.
原因是,我在同一个 Maven 项目中的其他文件中进行了更改,但没有被选中。
I solved it by building at the project level (Select the Maven project in intellij and press ctrl+shift+F9)
我通过在项目级别构建解决了它(在intellij中选择Maven项目并按ctrl+shift+F9)
回答by plosco
Just got this myself but non of those things were my issue. I downloaded a project from github, then opened in intelli J with the problem after trying to run a junit test.
我自己刚拿到这个,但这些都不是我的问题。我从github下载了一个项目,然后在尝试运行junit测试后在intelli J中打开了这个问题。
Solution: Project Structure -> Modules -> 'Project_Name' -> Sources (tab) -> click src folder -> Mark as: Sources
解决方案:项目结构 -> 模块 -> 'Project_Name' -> Sources (tab) -> 点击 src 文件夹 -> 标记为:Sources
This should highlight src to be blue and all the class files should be the circles with C instead of looking like normal files. Try running a test again and see if that does it.
这应该将 src 突出显示为蓝色,并且所有类文件都应该是带有 C 的圆圈,而不是看起来像普通文件。尝试再次运行测试,看看是否可以。