Eclipse 无法识别 Scala 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8522149/
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
Eclipse not recognizing Scala code
提问by Joe
I have Eclipse Indigo with the Scala IDE plugin. I downloaded a lift project from Maven. It builds correctly. Eclipse says it is viewing it with the Scala editor.
我有带有 Scala IDE 插件的 Eclipse Indigo。我从 Maven 下载了一个电梯项目。它正确构建。Eclipse 表示它正在使用 Scala 编辑器查看它。
However, it is still giving my an error on each line because it is treat the code as Java (it gives errors for no semicolon, for the word "def" etc.) I tried cleaning the project and it still gives the errors.
但是,它仍然在每一行上给我一个错误,因为它将代码视为 Java(它给出了没有分号、“def”等词的错误)我尝试清理项目,但它仍然给出错误。
回答by Luigi Plinge
A couple of things I would try:
我会尝试几件事:
1) Right-click on project, Configure > Add Scala Nature.
1) 右键单击项目,配置 > 添加 Scala Nature。
2) In the Scala menu, Run Setup Diagnositics... > Use recommended default settings
2) 在 Scala 菜单中,运行设置诊断... > 使用推荐的默认设置
回答by Mirco Dotta
Open the Package Explorer, look at your scala source. Does the icon displays a S
(for Scala), or a J
(for Java)?
打开包资源管理器,查看您的 Scala 源。图标显示的是S
(对于 Scala)还是J
(对于 Java)?
If you see a S
, then you are likely missing the Scala Nature. As Luigi suggested, try to add the Scala Nature and see if that fixes your issue (Right-click on project, Configure > Add Scala Nature.)
如果您看到S
,那么您可能错过了 Scala Nature。正如 Luigi 建议的那样,尝试添加 Scala Nature 并查看是否能解决您的问题(右键单击项目,配置 > 添加 Scala Nature。)
Otherwise, if you see a J
, the odds are that "JDT Weaving" is not enabled. That should not happen and might depend on other plugins you have installed.
否则,如果您看到J
,则很可能未启用“JDT Weaving”。这不应该发生,并且可能取决于您安装的其他插件。
What I would try if I were you is simply uninstalling all Scala IDE plugins from Eclipse ("Scala IDE for Eclipse", and also the "JDT Weaving for Scala" if you see it in the list of installed plugins). Then, reinstall Scala IDE 2.0.0-RC04. I know, it's not the ideal solution, but it's hard to tell what is going on without more information.
如果我是您,我会尝试从 Eclipse 中卸载所有 Scala IDE 插件(“Eclipse 的 Scala IDE”,如果您在已安装的插件列表中看到它,还有“JDT Weaving for Scala”)。然后,重新安装 Scala IDE 2.0.0-RC04。我知道,这不是理想的解决方案,但如果没有更多信息,很难判断发生了什么。
If that doesn't work either, please write a message in the scala-ide-user Mailing List. Make sure to provide the full list of plugins you have in your Eclipse installation. That might help us understand what is going on.
如果这也不起作用,请在scala-ide-user Mailing List 中写一条消息。确保提供您在 Eclipse 安装中拥有的插件的完整列表。这可能有助于我们了解正在发生的事情。
回答by srirachapills
This problem can show up when you copy an existing java project to start a new project or use a project file generator.
当您复制现有的 java 项目以启动新项目或使用项目文件生成器时,可能会出现此问题。
One thing you can look at is your Java Build Path
inside project properties.
Make sure your scala source folder doesn't have an include filter like **/*.java
.
Make sure it is **/*.scala
or remove it and your source should be detected by the scala plugin.
您可以查看的一件事是您的Java Build Path
内部项目属性。确保您的 Scala 源文件夹没有像**/*.java
. 确保它是**/*.scala
或删除它,并且 scala 插件应该检测到您的来源。
回答by trigoman
It may even be simpler than that, I forgot to write the package name at the top of the source file thus Eclipse neglected to see it as a Scala file.
它甚至可能比这更简单,我忘记在源文件的顶部写包名,因此 Eclipse 忽略了将其视为 Scala 文件。
回答by Bacchus
I solved the problem with compile/build errors with sbt-eclipse by
我用 sbt-eclipse 解决了编译/构建错误的问题
- Right-click project
- Scala menu -chose New Scala Installation and select version until no errors were listed
- 右键单击项目
- Scala 菜单 - 选择新 Scala 安装并选择版本,直到没有列出错误
回答by anupam mishra
I was facing the same problem, because in project scala folder was not included followed following steps project -> properties -> java built path -> source -> add folder
我遇到了同样的问题,因为在项目中没有包含 scala 文件夹,请按照以下步骤项目 -> 属性 -> java 构建路径 -> 源 -> 添加文件夹
回答by Sat
I use Maven to generate my jar. So followed the steps below Right click on project -> Maven -> Update Project -> Check force Update of Snapshots/Releases -> OK
我使用 Maven 生成我的 jar。所以按照下面的步骤右键单击项目 -> Maven -> 更新项目 -> 检查快照/版本的强制更新 -> 确定
回答by xmar
Sometimes, or in some versions, you don't see the "Add Scala Nature" option.
有时,或在某些版本中,您看不到“添加 Scala Nature”选项。
Adding it to your .project file manually should do. E.g. this project supports scala and java natures, so it will treat .java as Java, and .scala as Scala.
手动将它添加到您的 .project 文件应该可以。例如,这个项目支持 scala 和 java 性质,所以它会将 .java 视为 Java,将 .scala 视为 Scala。
<projectDescription>
<name>myprojectname</name>
<buildSpec>
<buildCommand>
<name>org.scala-ide.sdt.core.scalabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.scala-ide.sdt.core.scalanature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<linkedResources> </linkedResources>
</projectDescription>
回答by Fábio Sena
My Java Classes refuse to recognize my Scala ones. However when I do a "Ctrl + Click" = Open Declaration, it goes to the right place. My Java Classes that depend on Scala look like Red all over. I checked Build Path settings etc, they all look ok.
我的 Java 类拒绝识别我的 Scala 类。但是,当我执行“Ctrl + 单击”= 打开声明时,它会转到正确的位置。我依赖 Scala 的 Java 类看起来像红色。我检查了构建路径设置等,它们看起来都不错。
I had the same problem. When I moved my Scala classes to the correct package folder, it worked. Scala does not need this. But only that way Java editor reconized the Scala class.
我有同样的问题。当我将 Scala 类移动到正确的包文件夹时,它起作用了。Scala 不需要这个。但只有这样,Java 编辑器才能重新定义 Scala 类。
回答by jamie
I've had this problem before as well when using Maven and Eclipse - I'll be working in the Scala perspective and everything is fine, and then at some point Indigo completely loses track of the fact that it's using Scala and is building my code with a Java compiler. No amount of fiddling with Build Path and configurations, cleaning projects or enabling the Scala nature seems to fix it.
我之前在使用 Maven 和 Eclipse 时也遇到过这个问题——我将在 Scala 的角度工作,一切都很好,然后在某个时候 Indigo 完全忘记了它正在使用 Scala 并且正在构建我的代码的事实使用 Java 编译器。没有多少摆弄构建路径和配置、清理项目或启用 Scala 性质似乎可以解决它。
The only solution I've come up with so far is to use IntelliJ when I use Maven, and use Scala Eclipse plugin only when using SBT to build projects. You can try converting your Maven POM to an SBT project, but you'll have to update that every time your dependencies change. Heiko Seeberger's sbt-eclipse plugin is pretty slick.
到目前为止,我想出的唯一解决方案是在使用 Maven 时使用 IntelliJ,仅在使用 SBT 构建项目时使用 Scala Eclipse 插件。您可以尝试将您的 Maven POM 转换为 SBT 项目,但每次您的依赖项更改时都必须更新它。Heiko Seeberger 的 sbt-eclipse 插件非常漂亮。