Mixing Scala and Java files in an Eclipse project
Asked Answered
G

2

19

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?

Glide answered 15/12, 2010 at 11:5 Comment(5)
Just in case, check for builders in the project properties windows and make sure Scala is checked.Linderman
Another wild guess, use different source folders. One for Scala and one for Java, never had any problems doing like that.Linderman
Thanks. Checked that Scala builder was selected and moved the Java files to a different folder. Didn't help :(Glide
If you remove all the .java files (or all the .scala files) does it work?Manche
I stumble at this thread from google search. From what I have found. It seems like the problem doesn't come from scala part. Here is what I did. 1.build project, 2. make java and scala files 3. add scala nature. 4. try to build it (with java file import some class from scala file). This gave me error, so I tried to remove all scala nature/class and modify java file to a vanilla helloworld program. it still gave me same error (no class found or sort of).Breadth
M
9

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*

Manche answered 15/12, 2010 at 18:18 Comment(5)
Ok, tried that. The .project was identical, the .lasspath quite different so I changed the .classpath to the one you gave above and ... no difference. I've deleted the project, reimported the files etc but it's not working :(Glide
My mistakethe natures were in the other order. Unfortunately changing that still hasn't done anything. It's still not building any class fieGlide
I can't think of anything I did workspace-wide. Doesn't seem anything applicable in .settings... oh, I'm using Java 1.5 on this project, if that matters (1.5.0_22 in particular). Eclipse itself is running in 1.6.Manche
Also, I assume you already know the trick of doing "Project | Clean..." on a somewhat regular basis? I did it repeatedly while tinkering with the config files.Manche
I back up @Rodney on the "Project | Clean...".Linderman
M
8

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

Menderes answered 12/4, 2012 at 20:10 Comment(1)
Hi Steve, I'm using m2e, do you know how to change the compilation order in the pom, I can only see the scala-compile-first option davidb.github.io/scala-maven-plugin/example_java.htmlAircrew

© 2022 - 2024 — McMap. All rights reserved.