Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing Scala and Java files in an Eclipse project

I'm probably doing something stupid, but I can't spot it.

I've installed Eclipse Helios (Helios because I couldn't get Glassfish support to work correctly using Gallileo) and the nightly build of the Scala Eclipse plugin for Helios

I've created a Scala project and added some files - a mix of Java and Scala.

They all seem syntactically correct - the Eclipse editor at least seems to know what language each file is, and reports correctly on syntax errors when I make them - but the Java files cannot find classes created in Scala. The IDE site seems to suggest this should all just work.

There are no class files in the bin directory for any of the Scala files (there are class files for each of the classes defined by the Java files) so it seems that for some reason the Scala files aren't being built. These missing class files would explain why the Java files don't see the classes.

What have I missed? How do I tell Eclipse to build those files?

like image 304
The Archetypal Paul Avatar asked Dec 15 '10 11:12

The Archetypal Paul


People also ask

Can we use Scala in Eclipse?

Scala IDE v3. 0 can be installed on both Eclipse 3.7 (Indigo) and Eclipse 4.2 (Juno), through different update sites. If you are using Eclipse 3.7 (Indigo), we recommend the latest 1.6.

How do I add Scala support to Eclipse?

Downloading and installing the Scala IDE for Eclipse First choose a version of the IDE from http://download.scala-ide.org. We recommend you choose 2.0. x, which comes with Scala 2.9. Copy the corresponding URL and then choose Help/Install New Software and paste the URL you just copied.

Which Eclipse version is best for Scala?

4.7. This release is available for Scala 2.12 (with support for Scala 2.10 and 2.11 projects in the same workspace) and is based on Eclipse 4.7 (Oxygen).


2 Answers

I'm also using Helios (because I've gone 64-bit) and found the plugin behaving a bit odd at times (though to be honest not much differently than in Galileo). When mixing Java and Scala, as you found, it would look like there's no problem, but there would be no compiled code. Or worse a Java class dependent on a Scala class wouldn't compile because the Scala class wasn't compiled -- but it wouldn't compile until all the errors were gone from the project, which of course wouldn't happen until it compiles... ug.

I began tinkering with the classpath and project files, and eventually got it to work, though I can't really say why. I swear I changed a file, then changed it back, and then it worked??

Anyway, here are my files:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>xxxxxxxxxxxxx</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
    <buildCommand>
        <name>org.scala-ide.sdt.core.scalabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
<natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
    <nature>org.scala-ide.sdt.core.scalanature</nature>
</natures>
</projectDescription>

and

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>

The critical thing seems to be that SCALA_CONTAINER come before JRE_CONTAINER, but... I wouldn't bet on it either. The order of the "natures" may be important, too.

It's been working for me now for a week or two, with daily updates, so... *fingers-crossed*

like image 158
Rodney Gitzel Avatar answered Oct 13 '22 01:10

Rodney Gitzel


I've noticed that Eclipse-IDE sometimes doesn't compile my Scala files when I create a new file. I have to select the project in Package Explorer and refresh it (F5). Also, you can change the compilation order (Java first vs. Scala first) in Eclipse by going to:

Window->Preferences->Scala->Compiler->Build Manager->compileorder

like image 28
stephen.hanson Avatar answered Oct 13 '22 01:10

stephen.hanson