Scala source files not recognized by compiler in Eclipse
Asked Answered
G

4

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>
Glossography answered 20/3, 2012 at 11:15 Comment(7)
Do you have a Scala plug-in installed in Eclipse? Out-of-the-box, Eclipse does not have support for Scala.Expert
@Jesper: yes I do. I have installed Scala-ide.Glossography
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).Baron
Thanks for the tip Jens! I might switch to IntelliJ eventually...Glossography
possible duplicate of Eclipse not recognizing Scala codeCantle
There is also #9128963.Cantle
Thanks all. I switched to Helios for the time being. It works better.Glossography
M
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>
Mycenae answered 20/3, 2012 at 12:6 Comment(5)
good point: I was not aware of this file. However the content is almost identical as you can see and I have compile errors...Glossography
I use version 3.7 of Eclipse. Do you think that might be problematic?Glossography
It might, as explained on the scala-ide homepage (scala-ide.org/download/current.html), the recommended version of eclipse is Helios (3.6).Continuant
I also use 3.7 so that is fine.Mycenae
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.Mycenae
C
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.

Cantle answered 20/3, 2012 at 12:34 Comment(1)
@Glossography 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.Mycenae
M
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>
Mycenae answered 20/3, 2012 at 12:6 Comment(5)
good point: I was not aware of this file. However the content is almost identical as you can see and I have compile errors...Glossography
I use version 3.7 of Eclipse. Do you think that might be problematic?Glossography
It might, as explained on the scala-ide homepage (scala-ide.org/download/current.html), the recommended version of eclipse is Helios (3.6).Continuant
I also use 3.7 so that is fine.Mycenae
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.Mycenae
N
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

Nerveless answered 31/5, 2012 at 16:28 Comment(0)
C
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.

Candycecandystriped answered 19/12, 2013 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.