Eclipse 自动完成在某些 Java 文件中不起作用

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

Eclipse autocomplete not working in some Java files

eclipseautocomplete

提问by m_a_khan

I have noticed that autocomplete is not working in some of java files in Eclipse. Also, the files where autocomplete is not working, display a hollow "J" as the icon for the Java file. The files where autocomplete is working, icon for java file is a filled "J"

我注意到自动完成功能在 Eclipse 中的某些 java 文件中不起作用。此外,自动完成功能不起作用的文件会显示一个空心的“J”作为 Java 文件的图标。自动完成工作的文件,java文件的图标是一个填充的“J”

I am wondering if someone can point out what went wrong all of suddent, why the change in icons and why autocomplete and syntax highlighting is turned-off in the files with a hollow "J" icon?

我想知道是否有人可以突然指出出了什么问题,为什么图标的变化以及为什么在带有空心“J”图标的文件中关闭了自动完成和语法突出显示?

Thanks.

谢谢。

update

更新

Basically, I was doing what VonC has suggested but Eclipse was not refreshing that it why I was thinking that VonC's suggestion isn't working, after doing a refresh, the problem resolved.

基本上,我正在按照 VonC 的建议进行操作,但 Eclipse 并没有刷新我为什么认为 VonC 的建议不起作用的原因,在刷新后,问题解决了。

采纳答案by VonC

In this following picture, MyClass.javahas a hollow J, because it is explicitly excluded from the sources to build:

在下图中,MyClass.java有一个空洞J,因为它被明确排除在要构建的源之外:

alt text

替代文字

Could you go to the properties of the project, "Java Build Path" Section, "Source" tab and see if some exclusion filter has been set ?
It is usual for instance to define:

你能转到项目的属性,“Java Build Path部分”,“ Source”选项卡,看看是否设置了一些排除过滤器?
例如,通常定义:

**/Test*.java

to exclude at first building any unit-test class (when you have a large set of sources and do not want to be presented with Test classes during auto-completion, or do not want them considered during source searches).

首先排除构建任何单元测试类(当您有大量源并且不想在自动完成期间显示测试类时,或者不希望在源搜索期间考虑它们时)。

回答by bronze

Since this question is highly ranked on Google, I will add a solution to fix general auto complete issue, not for 'hollow J' ones.

由于这个问题在 Google 上排名很高,我将添加一个解决方案来解决一般的自动完成问题,而不是针对“空心 J”问题。

Try Window(Windows/Linux) or Eclipse(OS X) -> Preferences -> Java -> Editor -> Content Assist -> Restore Defaults

尝试Window(Windows/Linux) 或Eclipse(OS X) -> Preferences -> Java -> Editor -> Content Assist -> Restore Defaults

also

Content Assist -> Advanced -> Restore Defaults

内容辅助 -> 高级 -> 恢复默认值

回答by tony gil

some answers (restore defaults) above do not work for some adt bundle installs as of jan '13.

自 13 年 1 月起,上面的某些答案(恢复默认值)不适用于某些 adt 包安装。

in those cases, go to

在这些情况下,请转到

Window -> Preferences -> Java -> Editor -> Content Assist -> Advanced

窗口 -> 首选项 -> Java -> 编辑器 -> 内容辅助 -> 高级

and tick on the JAVA PROPOSAL options.

并勾选 JAVA PROPOSAL 选项。

回答by Chris Dennett

Those with a hollow J aren't part of the build path of the project, so they can't participate in the normal build process and therefore auto-complete won't be enabled for these files (and other Java editor features!). You must add the folders with the Java files to be built to the build path using the 'source path' section of the project properties. This can be accessed by right clicking on a project in the project navigator / package explorer and going to Properties. See http://www.informit.com/articles/article.aspx?p=367962for details.

那些带有空心 J 的文件不是项目构建路径的一部分,因此它们不能参与正常的构建过程,因此不会为这些文件(以及其他 Java 编辑器功能!)启用自动完成功能。您必须使用项目属性的“源路径”部分将包含要构建的 Java 文件的文件夹添加到构建路径。这可以通过右键单击项目导航器/包浏览器中的项目并转到属性来访问。有关详细信息,请参阅http://www.informit.com/articles/article.aspx?p=367962

Additionally, if the files aren't part of a Java project in the first place, you must create a project and move the files in, or put the files in an existing one. Again, make sure these file are under a source directory as described by that section of the project properties.

此外,如果这些文件一开始就不是 Java 项目的一部分,那么您必须创建一个项目并将这些文件移入,或者将这些文件放入现有的项目中。再次确保这些文件位于项目属性该部分所述的源目录下。

alt text

替代文字

回答by widmore

I cope with the issue by deleting the file if autocomplete does not work. Just before copying the source code. Then I have recreated the java file with the same name. Pasted the source code previously copied.

如果自动完成不起作用,我通过删除文件来解决这个问题。就在复制源代码之前。然后我重新创建了同名的java文件。粘贴之前复制的源代码。

回答by Kraagenskul

As an addendum to the @VonC answer, make sure that the Java files are part of the Inclusion pattern. I had a case where a build process was creating the project while only including .js files and not the Java files:

作为@VonC 答案的附录,请确保 Java 文件是包含模式的一部分。我有一个案例,构建过程正在创建项目,同时只包含 .js 文件而不是 Java 文件:

Shows Eclipse Include without Java files

显示没有 Java 文件的 Eclipse Include

You can manually edit the inclusions via the Edit button. In my case, a fix was made to the build script to make it permanent.

您可以通过编辑按钮手动编辑内含物。就我而言,对构建脚本进行了修复以使其永久化。

回答by Witold Kaczurba

Make sure you have the right directory structure. I believe that:

确保您拥有正确的目录结构。我相信:

  • Hollow J icon beside Java file - will not be build
  • Normal J icon beside Java file - is a file to be build.
  • Java 文件旁边的空心 J 图标 - 不会被构建
  • Java 文件旁边的普通 J 图标 - 是要构建的文件。

I made a mistake when I created webapp artefact. By default it does not create folder for Java, but for resources. I mistakenly put my sources there. Have a look - see the difference.

我在创建 webapp 人工制品时犯了一个错误。默认情况下,它不会为 Java 创建文件夹,而是为资源创建文件夹。我错误地把我的来源放在那里。看看 - 看看有什么不同。

enter image description here

在此处输入图片说明

回答by Dave Berry

Neither Restoring Defaults or my build path (file was already in package directory w/other files where auto-complete was working) fixed problem. Fix for me was to close the file explicitly (right click on file name in tab) and re-open. Interestingly, just re-starting Eclipse didn't work either.

恢复默认值或我的构建路径(文件已经在包目录中,其他文件在自动完成工作中)都没有修复问题。对我来说修复是明确关闭文件(右键单击选项卡中的文件名)并重新打开。有趣的是,只是重新启动 Eclipse 也不起作用。