Java IntelliJ 找不到任何声明

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

IntelliJ cannot find any declarations

javaintellij-ideaintellij-idea-2016

提问by Andy

I completely uninstalled IntelliJ and have now reinstalled and imported my project. I imported using gradle and can see all of the files in my project present. However, when I open a file I can't find any declaration to go to by doing cmd + click. I have the Java 7 SDK setup, and I can't even go to the declaration of classes in my own project. Going to a Java core class like Stringor Listdoesn't work either. How can I enable the IDE to be able to find my classes?

我完全卸载了 IntelliJ,现在重新安装并导入了我的项目。我使用 gradle 导入,可以看到我项目中的所有文件。但是,当我打开一个文件时,我无法通过执行 cmd + click 找到任何要转到的声明。我有 Java 7 SDK 设置,我什至无法进入我自己项目中的类声明。转到 Java 核心类,例如StringList也不行。如何让 IDE 能够找到我的类?

I'm on OSX Yosemite, IntelliJ 2016.1.2 build 145.972.

我在 OSX Yosemite 上,IntelliJ 2016.1.2 build 145.972。

回答by Thomas

I had this same problem, and @AniaG's solution in the comments worked for me.

我遇到了同样的问题,@AniaG 在评论中的解决方案对我有用。

  • Right-click srcfolder
  • Mark Directory as> Sources Root
  • 右键单击src文件夹
  • 将目录标记为>源根

enter image description here

在此处输入图片说明

回答by sujeet

Your source folders where your Symbols are (Classes) are need to be configured as "Content Root".

您的符号所在的源文件夹(类)需要配置为“内容根”。

  • Open Project Structure: #;
  • Click Modules
  • You can configure your Content Root (Sources, Tests, Resources etc) for each module that you want to Navigate to.
  • 打开项目结构: #;
  • 单击模块
  • 您可以为要导航到的每个模块配置内容根(源、测试、资源等)。

Once done you should be able to navigate to your symbols.

完成后,您应该能够导航到您的符号。

回答by mykey

IDEA may ignore some of your maven dependency files. The "External Libraries" node in your project structure might be empty or incomplete.

IDEA 可能会忽略您的某些 maven 依赖文件。项目结构中的“外部库”节点可能为空或不完整。

Go to:

去:

  1. IntelliJ --> Preferences
  2. Search for Maven [on the left tab] -> Ignored Files
  3. See if there is any path on the right side that is checked(ignored) and uncheck that.enter image description here
  1. IntelliJ --> 首选项
  2. 搜索 Maven [在左侧选项卡上] -> 忽略的文件
  3. 查看右侧是否有任何路径被选中(忽略)并取消选中。在此处输入图片说明

回答by Figo Wang

I found this cannot find declaration to go toproblem once in my Maven project. The reason for this was just that one of the sub-projects in my project did not import as maven project correctly.

cannot find declaration to go to在我的 Maven 项目中发现过这个问题。原因只是我项目中的一个子项目没有正确导入为 maven 项目。

回答by user8382868

I faced the same issue and spent almost 15-16 tiring hours to clean, rebuild, invalidate-cache, upgrade Idea from 16.3 to 17.2, all in vain. We have a Maven managed project and the build used to be successful but just couldn't navigate between declaration/implementations as Idea couldn't see the files.

我遇到了同样的问题,花了将近 15-16 个小时来清理、重建、使缓存失效、将 Idea 从 16.3 升级到 17.2,但都是徒劳的。我们有一个 Maven 管理的项目,并且构建曾经是成功的,但由于 Idea 看不到文件,因此无法在声明/实现之间导航。

After endlessly trying to fix this, it finally dawned to me that it's the IDEA settings causing all the headache. This is what I did (Windows system):

在无休止地尝试解决此问题后,我终于明白是 IDEA 设置导致了所有头痛。这就是我所做的(Windows系统):

  1. Exit IDE
  2. Recursively delete all .imlfiles from project directory del /s /q "C:\Dev\trunk\*.iml"
  3. Find and delete all .ideafolders
  4. Delete contents of the caches, index, and LocalHistory folders under <user_home>\.IntelliJIdea2017.2\system
  5. Open Idea and import project ....
  1. 退出IDE
  2. 递归删除.iml项目目录中的所有文件 del /s /q "C:\Dev\trunk\*.iml"
  3. 查找并删除所有.idea文件夹
  4. 删除 caches、index 和 LocalHistory 文件夹下的内容 <user_home>\.IntelliJIdea2017.2\system
  5. 打开Idea并导入项目....

VOILAAAAAAAAAAAA...!! I hope this helps a poor soul in pain

呜呜呜呜呜呜呜呜呜呜呜呜!!我希望这能帮助一个痛苦的可怜灵魂

回答by Daniel Reina

In my case I was using a gradle project with subprojects. The reason IntelliJ couldn't find declarations was that the subprojects were not built.

就我而言,我使用的是带有子项目的 gradle 项目。IntelliJ 找不到声明的原因是未构建子项目。

After investigating why subprojects were not built, I found out that using "auto-import" was breaking the subprojects.

在调查了为什么没有构建子项目之后,我发现使用“自动导入”会破坏子项目。

After importing the project from build.gradlewithout auto-import, it worked for me.

build.gradle没有自动导入的情况下导入项目后,它对我有用。

回答by mithunpaul

For what its worth, in Pycharm it is: Right click on the root folder->Mark Directory as-> Sources Root

就其价值而言,在 Pycharm 中它是:右键单击根文件夹-> 将目录标记为-> 源根

回答by Anand

Ensure that the directory which contains your compiled classes and libraries is marked as sources Root.

确保包含已编译类和库的目录被标记为源根。

回答by Vic.Liu

If you see red circle on the icon of Java files, and got warning like "cannot find declaration to go to". Then you can do something below:

如果您在 Java 文件的图标上看到红色圆圈,并收到“找不到要转到的声明”之类的警告。然后你可以在下面做一些事情:

  1. Right click root folder of your project/module.
  2. Mark Directory As -> Excluded.
  1. 右键单击项目/模块的根文件夹。
  2. 将目录标记为 -> 已排除。

enter image description here

在此处输入图片说明

It works for me. in Windows 7, Intellij 2016.1.1

这个对我有用。在 Windows 7 中,Intellij 2016.1.1

回答by notihs

Had the same problem. But only on my own methods.
Just fixed it by invalidating cache: (File-> Invalidate Caches/Restart)

有同样的问题。但仅限于我自己的方法。
只是通过使缓存无效来修复它:(文件-> 使缓存无效/重新启动)