5

I am working on a playframework project that I have eclipsified from the console and opened from Eclipse.

However my scala source files are treated as java source files by Eclipse with the resulting compilation errors.

Can anyone please help?

EDIT: My .project file:

<projectDescription>
  <name>web</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>
</projectDescription>
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
balteo
  • 23,602
  • 63
  • 219
  • 412
  • Do you have a Scala plug-in installed in Eclipse? Out-of-the-box, Eclipse does not have support for Scala. – Jesper Mar 20 '12 at 11:51
  • @Jesper: yes I do. I have installed Scala-ide. – balteo Mar 20 '12 at 11:58
  • 1
    This is not an answer, but I just wanted to point out that I have had numerous problems with Eclipse and Scala. Eventually I got too frustrated and changed IDE. If you more persistent than me I am certain it can be fixed, but I truly recommend using something else (IntelliJ works great for me). – Jens Egholm Mar 20 '12 at 12:15
  • Thanks for the tip Jens! I might switch to IntelliJ eventually... – balteo Mar 20 '12 at 12:17
  • possible duplicate of [Eclipse not recognizing Scala code](http://stackoverflow.com/questions/8522149/eclipse-not-recognizing-scala-code) – Matthew Farwell Mar 20 '12 at 12:31
  • There is also http://stackoverflow.com/questions/9128463/scala-eclipse-ide-strange-behavior. – Matthew Farwell Mar 20 '12 at 12:32
  • 1
    Thanks all. I switched to Helios for the time being. It works better. – balteo Mar 20 '12 at 12:54

4 Answers4

5

The official supported version for Scala-ide is Helios, the Indigo version is still classed as experimental. However, you could try adding Scala Nature to your project. Right click on the Project, Select Configure->Add Scala Nature, or look at the ideas in one of the two questions I've linked to.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • @balteo Since you already have the Scala Nature in your .project. The `Configure` menu should not contain the `Add Scala Nature` item. If it does, that would be an indication that your eclipse is not in sync with the config files. So that is an interesting test. – Joe23 Mar 20 '12 at 12:42
4

I have had similar issues in the past. I think the problem was the Java Builder for the project. This is the content of a .project file that can build Java as well as Scala classes. You can compare it to your own configuration:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>debug-s</name>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.scala-ide.sdt.core.scalabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.scala-ide.sdt.core.scalanature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>
Joe23
  • 5,683
  • 3
  • 25
  • 23
  • good point: I was not aware of this file. However the content is almost identical as you can see and I have compile errors... – balteo Mar 20 '12 at 12:09
  • I use version 3.7 of Eclipse. Do you think that might be problematic? – balteo Mar 20 '12 at 12:12
  • It might, as explained on the scala-ide homepage (http://scala-ide.org/download/current.html), the recommended version of eclipse is Helios (3.6). – Nicolas Mar 20 '12 at 12:23
  • I also use 3.7 so that is fine. – Joe23 Mar 20 '12 at 12:24
  • 2
    Your configuration looks fine. Maybe Eclipse has an old version in memory. Can you do the "eclipse dance"? That is `right click on project > Refresh` `Select Project > (Menu-Bar) Project > Clean...` `Select Project > Close Project > Open Project`. It sounds stupid, but in particular with the scala plugin I have often had problems where old build artifacts where the problem. – Joe23 Mar 20 '12 at 12:28
2

I went through the same issue yesterday, TWICE, in a project that was working just fine (so my project natures were fine). Both were triggered by running Maven from Eclipse with the eclipse:eclipse goal (to add references to new jars), which changed around my .classpath file and made all my .scala files red. Reverting .classpath to match my previously-working version (minus the new jars) wouldn't fix the compile errors. I restarted Eclipse, rebuilt, cleaned, pulled the Java and Scala natures in and out of the .project file, diffed everything against versions that had worked an hour before... still all red. Very frustrating.

I don't remember exactly what combination of those steps fixed things the first time yesterday, but just now I came in, did the clean, restart, etc, still all the Scala files are broken. Sigh. Then "hmmm" and I edited one of the .scala files. THAT triggered a "real" build, and now everything is fine. ?!?!?

TL;DR if your project was fine previously, and now ALL your .scala files are broken, try making a trivial edit to one of them, it worked for me

Rodney Gitzel
  • 2,652
  • 16
  • 23
0

I was having the same issue. I'm using Eclipse 4.3 (Kepler) and
Scala IDE for Eclipse 3.0.1.v-2_10-201307101641-eff8147. I have a maven scala project that I eclipsified and imported. It was working just fine, but when I import java packages, the whole file is no longer compilable. I was able to get it to compile again by changing the order of the libraries. But then that trick no longer worked and all of my scala files become uncompilable. I tried all of the things suggested here (the Eclipse dance) to no avail.

Then I tried creating a new scala project in Eclipse. I copied over the .classpath file and the src directory from the original project - et voila! Now it compiles just fine. There must be a problem in the maven-generated eclipse files.

ETA: I checked my .project file created by maven. It had this specified in it: <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> </buildCommand>

And when I took it out, the problem disappeared. I also had another problem: <comment>myproject. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>

Which is probably why the javabuilder appeared in the first place. I had to add in the scalabuilder by adding Scala Nature to my project. Maybe this problem won't appear of I add the M2Eclipse plugin. I will try it and see.