简体   繁体   English

Eclipse无法识别Scala代码

[英]Eclipse not recognizing Scala code

I have Eclipse Indigo with the Scala IDE plugin. 我有Eclipse Indigo和Scala IDE插件。 I downloaded a lift project from Maven. 我从Maven下载了一个电梯项目。 It builds correctly. 它构建正确。 Eclipse says it is viewing it with the Scala editor. 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”等)我尝试清理项目并且它仍然给出了错误。

A couple of things I would try: 我会尝试几件事:

1) Right-click on project, Configure > Add Scala Nature. 1)右键单击项目,配置>添加Scala自然。

2) In the Scala menu, Run Setup Diagnositics... > Use recommended default settings 2)在Scala菜单中,运行Setup Diagnositics ...>使用推荐的默认设置

Open the Package Explorer, look at your scala source. 打开Package Explorer,查看scala源代码。 Does the icon displays a S (for Scala), or a J (for Java)? 图标是显示S (对于Scala)还是J (对于Java)?

If you see a S , then you are likely missing the Scala Nature. 如果你看到S ,那么你可能会错过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.) 正如Luigi建议的那样,尝试添加Scala Nature,看看是否能解决您的问题(右键单击项目,配置>添加Scala自然。)

Otherwise, if you see a J , the odds are that "JDT Weaving" is not enabled. 否则,如果您看到J ,则可能无法启用“JDT Weaving”。 That should not happen and might depend on other plugins you have installed. 这不应该发生,可能取决于您安装的其他插件。

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). 如果我是你,我会尝试简单地卸载Eclipse中的所有Scala IDE插件(“Scala IDE for Eclipse”,以及“JDT Weaving for Scala”,如果你在安装的插件列表中看到它)。 Then, reinstall Scala IDE 2.0.0-RC04. 然后,重新安装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. 我知道,这不是理想的解决方案,但如果没有更多信息,很难说出发生了什么。

If that doesn't work either, please write a message in the scala-ide-user Mailing List . 如果这也不起作用,请在scala-ide-user邮件列表中写一条消息。 Make sure to provide the full list of plugins you have in your Eclipse installation. 确保提供Eclipse安装中的插件的完整列表。 That might help us understand what is going on. 这可能有助于我们了解正在发生的事情。

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. 您可以看到的一件事是项目属性中的Java Build Path Make sure your scala source folder doesn't have an include filter like **/*.java . 确保您的scala源文件夹没有像**/*.java这样的包含过滤器。 Make sure it is **/*.scala or remove it and your source should be detected by the scala plugin. 确保它是**/*.scala或删除它,scala插件应检测到你的源代码。

它甚至可能比这简单,我忘了在源文件的顶部编写包名,因此Eclipse忽略了将其视为Scala文件。

I solved the problem with compile/build errors with sbt-eclipse by 我用sbt-eclipse解决了编译/构建错误的问题

  1. Right-click project 右键单击项目
  2. Scala menu -chose New Scala Installation and select version until no errors were listed Scala菜单 - 选择新Scala安装并选择版本,直到没有列出错误

我遇到了同样的问题,因为在项目scala文件夹中没有包含以下步骤项目 - > properties - > java built path - > source - > add folder

I have similar issues, though when I remove the Scala Nature and Add it back it becomes ok. 我有类似的问题,但是当我删除Scala Nature并将其添加回来时它就变得可以了。 Which is fine for now. 现在这很好。

But there is another issue: 但还有另一个问题:

My Java Classes refuse to recognize my Scala ones. 我的Java Classes拒绝识别我的Scala。 However when I do a "Ctrl + Click" = Open Declaration, it goes to the right place. 但是,当我执行“Ctrl + Click”=打开声明时,它会转到正确的位置。 My Java Classes that depend on Scala look like Red all over. 依赖于Scala的我的Java类看起来像Red一样。 I checked Build Path settings etc, they all look ok. 我检查了Build Path设置等,它们看起来都很好。

My Java Classes refuse to recognize my Scala ones. 我的Java Classes拒绝识别我的Scala。 However when I do a "Ctrl + Click" = Open Declaration, it goes to the right place. 但是,当我执行“Ctrl + Click”=打开声明时,它会转到正确的位置。 My Java Classes that depend on Scala look like Red all over. 依赖于Scala的我的Java类看起来像Red一样。 I checked Build Path settings etc, they all look ok. 我检查了Build Path设置等,它们看起来都很好。

I had the same problem. 我有同样的问题。 When I moved my Scala classes to the correct package folder, it worked. 当我将Scala类移动到正确的包文件夹时,它工作正常。 Scala does not need this. Scala不需要这个。 But only that way Java editor reconized the Scala class. 但只有Java编辑器才重新调整Scala类。

I use Maven to generate my jar. 我用Maven来生成我的罐子。 So followed the steps below Right click on project -> Maven -> Update Project -> Check force Update of Snapshots/Releases -> OK 所以按照以下步骤右键单击项目 - > Maven - >更新项目 - >检查强制更新快照/版本 - >确定

Sometimes, or in some versions, you don't see the "Add Scala Nature" option. 有时,或者在某些版本中,您没有看到“添加Scala自然”选项。

Adding it to your .project file manually should do. 手动将其添加到.project文件中应该可以。 Eg this project supports scala and java natures, so it will treat .java as Java, and .scala as Scala. 例如,这个项目支持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>

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. 在使用Maven和Eclipse之前我也遇到过这个问题 - 我将在Scala视角下工作,一切都很好,然后在某些时候,Indigo完全失去了它正在使用Scala并构建我的代码的事实使用Java编译器。 No amount of fiddling with Build Path and configurations, cleaning projects or enabling the Scala nature seems to fix it. 没有任何摆弄构建路径和配置,清理项目或启用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. 到目前为止,我提出的唯一解决方案是在使用Maven时使用IntelliJ,并且仅在使用SBT构建项目时才使用Scala Eclipse插件。 You can try converting your Maven POM to an SBT project, but you'll have to update that every time your dependencies change. 您可以尝试将Maven POM转换为SBT项目,但每次依赖项更改时都必须更新。 Heiko Seeberger's sbt-eclipse plugin is pretty slick. Heiko Seeberger的sbt-eclipse插件很漂亮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM