由于“找不到符号类 X”错误,无法在 Intellij Idea 中编译 java 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20314268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:00:52  来源:igfitidea点击:

Can't compile java class in Intellij Idea due to "cannot find symbol class X" error

javaintellij-idea

提问by gumkins

I created new Java module from scratch in my project. "create 'src' folder" was selected in module creation wizard. There is single "com" package in source folder which contains two public classes (say A and B). A class instantiates B class. IDE does not show errors and B class source is opened on "ctrl+B" pressing when cursor is located on the class name inside A class source. But when I'm trying to compile A class (by context menu -> "compile A.java class") following error is shown

我在我的项目中从头开始创建了新的 Java 模块。在模块创建向导中选择了“创建 'src' 文件夹”。源文件夹中有一个“com”包,其中包含两个公共类(比如 A 和 B)。A类实例化B类。当光标位于 A 类源中的类名上时,IDE 不会显示错误并且 B 类源在按下“ctrl+B”时打开。但是当我尝试编译 A 类时(通过上下文菜单 - >“编译 A.java 类”)显示以下错误

Error:(15, 20) cannot find symbol class B

错误:(15, 20) 找不到符号类 B

"src" folder is marked as source root.

“src”文件夹被标记为源根。

It looks like IDE bug.

它看起来像IDE错误。

Someone suggested to do "File->Invalidate Caches" to fix alike issue also asked on SO. I tried that and it did not help.

有人建议执行“文件->无效缓存”来解决 SO 上也提出的类似问题。我试过了,但没有帮助。

What possible reason of that and how it can be fixed?

什么可能的原因以及如何解决?

--

——

Intellij Idea Ultimate 11.1.5.

Intellij Idea Ultimate 11.1.5。

IDE Java: 1.7.0_45

IDE Java:1.7.0_45

Project SDK: 1.6.0_45

项目 SDK:1.6.0_45

采纳答案by Micha? Rybak

Try compiling the whole project (or at least class B) first.

首先尝试编译整个项目(或至少是 class B)。

If you compile onlyclass A, class Bcannot be found because it hasn't been compiled yet (therefore, no bytecode B.classfile exists).

如果编译class AB则找不到class ,因为它尚未编译(因此,不B.class存在字节码文件)。

This is not intuitive, but true. IDEA doesn't automatically compile dependant classes when you compile single class.

这不是直观的,而是真实的。编译单个类时,IDEA 不会自动编译依赖类。

回答by Qoph

If a class within a package is dependent on another class in the package, you need to create a classpath. Otherwise, the compiler does not know that the other class actually exists. Your IDE probably knows inherently that the classes are in the same package, which is why it isn't showing any errors.

如果包中的类依赖于包中的另一个类,则需要创建类路径。否则,编译器不知道另一个类实际存在。您的 IDE 可能天生就知道这些类在同一个包中,这就是它不显示任何错误的原因。

You can resolve this issue most simply by supplying both classes as arguments to the compiler (I'm not familiar with the tool you're using to compile so this may not be possible), or by compiling a class outside the package that imports package 'com'. Both of these will create the classpath file along with the class files.

您可以通过将两个类作为参数提供给编译器(我不熟悉您用来编译的工具,因此这可能不可能),或者通过在导入包的包之外编译一个类来解决这个问题'com'。这两者都将创建类路径文件以及类文件。

回答by Vouze

"right-click on project -> compile"

“右键单击项目-> 编译”

This did solve the same problem for me.

这确实为我解决了同样的问题。

回答by Javier Rodriguez

Select Build -> Rebuild Project, it works for me!

选择 Build -> Rebuild Project,它对我有用!

回答by kfkhalili

Right click project: Maven -> Reimport

右键项目:Maven -> Reimport

This worked on Intellij Mac.

这适用于 Intellij Mac。