在Intellij的Java项目中获取“找不到符号”

时间:2023-01-25 19:57:06

I make this call to a static singleton instance from the class "GameManager.java".

我从类“GameManager.java”调用一个静态单例实例。

HUD.getInstance().update(timeDelta);

HUD.java contains the HUD class as well as two other related classes, HUDTextElement and HUDElement. All the classes are in the same root path ../src/org/mypackage

HUD.java包含HUD类以及其他两个相关的类,HUDTextElement和HUDElement。所有类都在相同的根路径../src/org/mypackage

However, when compiling this java project in IntelliJ I get "cannot find Symbol HUD" on the line I make the HUD.getInstance() call.

但是,当在IntelliJ中编译这个java项目时,我会在我执行HUD.getInstance()调用的行上找到“找不到符号HUD”。

This exact same code compiles just fine in eclipse, any idea what the problem is?

这个完全相同的代码在eclipse中编译得很好,任何想法是什么问题?

13 个解决方案

#1


50  

I had the same problem, and turns out I had never completely compiled the fresh project. So right-clicking and selecting Compile'' (shift-cmd-F9 on mac) fixed it. It seems the compile on save does not 'see' non-compiled files.

我有同样的问题,结果我从来没有完全编译新项目。所以右键单击并选择Compile''(在mac上的shift-cmd-F9)修复它。似乎save上的编译没有“看到”非编译文件。

Marking the src folder as source did not help in my case.

将src文件夹标记为源代码在我的案例中没有帮助。

#2


38  

Select Build->Rebuild Project will solve it

选择Build-> Rebuild Project将解决它

#3


20  

This is likely to be your ../src folder is not marked as a "source" folder in Intellij IDEA, so it doesn't know to look there to find your class. You can right click the folder in the project explorer and choose "mark as source folder" to fix this.

这可能是您的../src文件夹未在Intellij IDEA中标记为“源”文件夹,因此它不知道在那里找到您的类。您可以右键单击项目资源管理器中的文件夹,然后选择“标记为源文件夹”来解决此问题。

#4


16  

I had the same problem and fixed it by clicking File>Invalidate caches/ restart

我遇到了同样的问题并通过单击文件>无效缓存/重新启动来修复它

#5


13  

I was getting the same "cannot find symbol" error when I did Build -> Make Project. I fixed this by deleting my Maven /target folder, right clicking my project module and doing Maven -> Reimport, and doing Build -> Rebuild Project. This was on IntelliJ Idea 13.1.5.

当我执行Build - > Make Project时,我得到了相同的“找不到符号”错误。我通过删除我的Maven / target文件夹,右键单击我的项目模块并执行Maven - > Reimport,并执行Build - > Rebuild Project来修复此问题。这是在IntelliJ Idea 13.1.5上。

It turns out the Maven -> Reimport was key, since the problem resurfaced a few times before I finally did that.

事实证明,Maven - > Reimport是关键,因为在我最终做到这一点之前问题重新出现了几次。

#6


10  

Thanks for the help so far, turns out the fix was to compile HUD.java first (right click on the file-> Compile HUD.java). After compiling the java file the rest of the project could be compiled without any problems.

感谢您的帮助到目前为止,事实证明修复是首先编译HUD.java(右键单击文件 - >编译HUD.java)。编译java文件后,可以编译项目的其余部分而不会出现任何问题。

I don't really know why this fixed it, or why IntelliJ wouldn't do this automatically, but root error seems it has to do with IntelliJ not correctly handling having multiple classes in a single .java file.

我真的不知道为什么修复它,或者为什么IntelliJ不会自动执行此操作,但root错误似乎与IntelliJ无法正确处理在单个.java文件中有多个类。

#7


5  

I use maven in my project. For some reason IntelliJ was giving me these kind of wierd errors. I ran mvn clean and tried a resync and these errors disappeared.

我在我的项目中使用maven。出于某种原因,IntelliJ给了我这些奇怪的错误。我运行mvn clean并尝试重新同步,这些错误消失了。

#8


2  

I know this is an old question, but as per my recent experience, this happens because the build resources are either deleted or Idea cannot recognize them as the source.

我知道这是一个老问题,但根据我最近的经验,这是因为构建资源被删除或者Idea无法将它们识别为源。

Wherever the error appears, provide sources for the folder/directory and this error must be resolved.

如果出现错误,请提供文件夹/目录的源,并且必须解决此错误。

Sometimes even when we assign sources for the whole folder, individual classes might still be unavailable. For novice users simple solution is to import a fresh copy and build the application again to be good to go.

有时即使我们为整个文件夹分配源,单个类仍可能不可用。对于新手用户来说,简单的解决方案是导入一个新的副本并再次构建应用程序以便更好。

It is advisable to do a clean install after this.

建议在此之后进行全新安装。

#9


0  

Since this is the first hit on Google searching for "intelliJ cannot find symbol" error, I'm gonna throw in my solution as well.

由于这是谷歌搜索“intelliJ无法找到符号”错误的第一个打击,我也会投入我的解决方案。

The problem for me was that my project originated from Eclipse, and some files contained dependency on classes that were generated in src/generated-sources by specifications in pom.xml. For some reason, this was not properly executed when I first opened the project and rebuilding/re-importing did not help, so the files were never generated.

对我来说问题是我的项目源自Eclipse,有些文件包含对prc.xml中的规范在src / generated-sources中生成的类的依赖。出于某种原因,当我第一次打开项目并且重建/重新导入没有帮助时,这没有正确执行,因此从未生成文件。

The solution was to right-click on the module, and select Maven -> Generate Sources and Update Folders That solved the issue and I could compile.

解决方案是右键单击模块,然后选择Maven - >生成源和更新文件夹解决了这个问题,我可以编译。

#10


0  

Make sure the source file of the java class you are trying to refer to has a .java extension. It was .aj in my case (I must have hit "Create aspect" instead of "Create class" when creating it). IntelliJ shows the same icon for this file as for "normal" class, but compiler does not see it when building.

确保您尝试引用的java类的源文件具有.java扩展名。在我的情况下是.aj(在创建时我必须点击“创建方面”而不是“创建类”)。 IntelliJ显示该文件与“普通”类相同的图标,但编译器在构建时不会看到它。

Changing .aj to .java fixed it in my case.

将.aj更改为.java会修复它。

#11


0  

Sometimes the class you want is in the test source directory. Happened to me, anyway…

有时您想要的类位于测试源目录中。无论如何......发生在我身上......

#12


-1  

I know this is old, but for anyone else, make sure that the class that's missing is in the same package as the class where you get the error/where your calling it from.

我知道这是旧的,但对于其他任何人,请确保缺少的类与您收到错误的类/调用它的类在同一个包中。

#13


-1  

I was also having the same problem. What solved for me was that I deleted the output folder (out, in my case) and built the project. This fixed the issue.

我也遇到了同样的问题。为我解决的是我删除了输出文件夹(在我的例子中),并构建了项目。这解决了这个问题。

#1


50  

I had the same problem, and turns out I had never completely compiled the fresh project. So right-clicking and selecting Compile'' (shift-cmd-F9 on mac) fixed it. It seems the compile on save does not 'see' non-compiled files.

我有同样的问题,结果我从来没有完全编译新项目。所以右键单击并选择Compile''(在mac上的shift-cmd-F9)修复它。似乎save上的编译没有“看到”非编译文件。

Marking the src folder as source did not help in my case.

将src文件夹标记为源代码在我的案例中没有帮助。

#2


38  

Select Build->Rebuild Project will solve it

选择Build-> Rebuild Project将解决它

#3


20  

This is likely to be your ../src folder is not marked as a "source" folder in Intellij IDEA, so it doesn't know to look there to find your class. You can right click the folder in the project explorer and choose "mark as source folder" to fix this.

这可能是您的../src文件夹未在Intellij IDEA中标记为“源”文件夹,因此它不知道在那里找到您的类。您可以右键单击项目资源管理器中的文件夹,然后选择“标记为源文件夹”来解决此问题。

#4


16  

I had the same problem and fixed it by clicking File>Invalidate caches/ restart

我遇到了同样的问题并通过单击文件>无效缓存/重新启动来修复它

#5


13  

I was getting the same "cannot find symbol" error when I did Build -> Make Project. I fixed this by deleting my Maven /target folder, right clicking my project module and doing Maven -> Reimport, and doing Build -> Rebuild Project. This was on IntelliJ Idea 13.1.5.

当我执行Build - > Make Project时,我得到了相同的“找不到符号”错误。我通过删除我的Maven / target文件夹,右键单击我的项目模块并执行Maven - > Reimport,并执行Build - > Rebuild Project来修复此问题。这是在IntelliJ Idea 13.1.5上。

It turns out the Maven -> Reimport was key, since the problem resurfaced a few times before I finally did that.

事实证明,Maven - > Reimport是关键,因为在我最终做到这一点之前问题重新出现了几次。

#6


10  

Thanks for the help so far, turns out the fix was to compile HUD.java first (right click on the file-> Compile HUD.java). After compiling the java file the rest of the project could be compiled without any problems.

感谢您的帮助到目前为止,事实证明修复是首先编译HUD.java(右键单击文件 - >编译HUD.java)。编译java文件后,可以编译项目的其余部分而不会出现任何问题。

I don't really know why this fixed it, or why IntelliJ wouldn't do this automatically, but root error seems it has to do with IntelliJ not correctly handling having multiple classes in a single .java file.

我真的不知道为什么修复它,或者为什么IntelliJ不会自动执行此操作,但root错误似乎与IntelliJ无法正确处理在单个.java文件中有多个类。

#7


5  

I use maven in my project. For some reason IntelliJ was giving me these kind of wierd errors. I ran mvn clean and tried a resync and these errors disappeared.

我在我的项目中使用maven。出于某种原因,IntelliJ给了我这些奇怪的错误。我运行mvn clean并尝试重新同步,这些错误消失了。

#8


2  

I know this is an old question, but as per my recent experience, this happens because the build resources are either deleted or Idea cannot recognize them as the source.

我知道这是一个老问题,但根据我最近的经验,这是因为构建资源被删除或者Idea无法将它们识别为源。

Wherever the error appears, provide sources for the folder/directory and this error must be resolved.

如果出现错误,请提供文件夹/目录的源,并且必须解决此错误。

Sometimes even when we assign sources for the whole folder, individual classes might still be unavailable. For novice users simple solution is to import a fresh copy and build the application again to be good to go.

有时即使我们为整个文件夹分配源,单个类仍可能不可用。对于新手用户来说,简单的解决方案是导入一个新的副本并再次构建应用程序以便更好。

It is advisable to do a clean install after this.

建议在此之后进行全新安装。

#9


0  

Since this is the first hit on Google searching for "intelliJ cannot find symbol" error, I'm gonna throw in my solution as well.

由于这是谷歌搜索“intelliJ无法找到符号”错误的第一个打击,我也会投入我的解决方案。

The problem for me was that my project originated from Eclipse, and some files contained dependency on classes that were generated in src/generated-sources by specifications in pom.xml. For some reason, this was not properly executed when I first opened the project and rebuilding/re-importing did not help, so the files were never generated.

对我来说问题是我的项目源自Eclipse,有些文件包含对prc.xml中的规范在src / generated-sources中生成的类的依赖。出于某种原因,当我第一次打开项目并且重建/重新导入没有帮助时,这没有正确执行,因此从未生成文件。

The solution was to right-click on the module, and select Maven -> Generate Sources and Update Folders That solved the issue and I could compile.

解决方案是右键单击模块,然后选择Maven - >生成源和更新文件夹解决了这个问题,我可以编译。

#10


0  

Make sure the source file of the java class you are trying to refer to has a .java extension. It was .aj in my case (I must have hit "Create aspect" instead of "Create class" when creating it). IntelliJ shows the same icon for this file as for "normal" class, but compiler does not see it when building.

确保您尝试引用的java类的源文件具有.java扩展名。在我的情况下是.aj(在创建时我必须点击“创建方面”而不是“创建类”)。 IntelliJ显示该文件与“普通”类相同的图标,但编译器在构建时不会看到它。

Changing .aj to .java fixed it in my case.

将.aj更改为.java会修复它。

#11


0  

Sometimes the class you want is in the test source directory. Happened to me, anyway…

有时您想要的类位于测试源目录中。无论如何......发生在我身上......

#12


-1  

I know this is old, but for anyone else, make sure that the class that's missing is in the same package as the class where you get the error/where your calling it from.

我知道这是旧的,但对于其他任何人,请确保缺少的类与您收到错误的类/调用它的类在同一个包中。

#13


-1  

I was also having the same problem. What solved for me was that I deleted the output folder (out, in my case) and built the project. This fixed the issue.

我也遇到了同样的问题。为我解决的是我删除了输出文件夹(在我的例子中),并构建了项目。这解决了这个问题。