在 Eclipse 项目中混合使用 Scala 和 Java 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4449218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixing Scala and Java files in an Eclipse project
提问by The Archetypal Paul
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
我已经安装了 Eclipse Helios(Helios,因为我无法使用 Gallileo 获得 Glassfish 支持以正常工作)以及 Helios 的 Scala Eclipse 插件的夜间构建
I've created a Scala project and added some files - a mix of Java and Scala.
我创建了一个 Scala 项目并添加了一些文件 - Java 和 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.
它们在语法上似乎都是正确的——Eclipse 编辑器至少似乎知道每个文件是什么语言,并且在我创建它们时正确报告语法错误——但是 Java 文件找不到在 Scala 中创建的类。IDE 站点似乎建议这一切都可以正常工作。
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.
bin 目录中没有任何 Scala 文件的类文件(Java 文件定义的每个类都有类文件),因此似乎由于某种原因没有构建 Scala 文件。这些缺失的类文件可以解释为什么 Java 文件看不到这些类。
What have I missed? How do I tell Eclipse to build those files?
我错过了什么?我如何告诉 Eclipse 构建这些文件?
采纳答案by Rodney Gitzel
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.
我也在使用 Helios(因为我已经使用了 64 位)并且发现该插件有时表现得有点奇怪(尽管说实话与 Galileo 没有太大区别)。当混合使用 Java 和 Scala 时,正如您发现的那样,看起来没有问题,但不会有编译代码。或者更糟糕的是,依赖于 Scala 类的 Java 类不会编译,因为 Scala 类没有被编译——但它不会编译,直到项目中的所有错误都消失了,这当然不会发生,直到它编译……呃。
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.
关键似乎是 SCALA_CONTAINER 出现在 JRE_CONTAINER 之前,但是......我也不会打赌。“性质”的顺序也可能很重要。
It's been working for me now for a week or two, with daily updates, so... *fingers-crossed*
它已经对我来说有效了一两个星期,每天都有更新,所以...... *手指交叉*
回答by stephen.hanson
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:
我注意到当我创建一个新文件时,Eclipse-IDE 有时不会编译我的 Scala 文件。我必须在包资源管理器中选择项目并刷新它 (F5)。此外,您可以通过以下方式更改 Eclipse 中的编译顺序(Java 优先与 Scala 优先):
Window->Preferences->Scala->Compiler->Build Manager->compileorder
窗口->首选项->Scala->编译器->构建管理器->编译顺序