如何在 IntelliJ IDEA 中使用 Scala(或:为什么为 Scala 获得一个有效的 IDE 如此困难)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2668153/
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
How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?
提问by Alex R
I recently gave up trying to use Scala in Eclipse (basic stuff like completion doesn't work). So now I'm trying IntelliJ. I'm not getting very far.
我最近放弃了在 Eclipse 中使用 Scala 的尝试(像完成这样的基本东西不起作用)。所以现在我正在尝试 IntelliJ。我不会走得很远。
I've been able to edit programs (within syntax highlighting and completion... yay!). But I'm unable to run even the simplest "Hello World". This was the original error:
我已经能够编辑程序(在语法高亮和完成中......是的!)。但是我什至无法运行最简单的“Hello World”。这是原来的错误:
Scala signature Predef has wrong version Expected 5.0 found: 4.1 in .... scala-library.jar
But that was yesterday with IDEA 9.0.1. See below...
但那是昨天的 IDEA 9.0.1。见下文...
UPDATE
更新
Today I uninstalled IntelliJ 9.0.1, and installed 9.0.2 Early Availability, with the 4/14 stable version of the Scala plug-in.
今天卸载了IntelliJ 9.0.1,安装了9.0.2 Early Availability,附带4/14稳定版Scala插件。
Then I setup a project from scratch through the wizards:
然后我通过向导从头开始设置一个项目:
- new project from scratch
- JDK is 1.6.u20
- accept the default (project) instead of global / module
- accept the download of Scala 2.8.0beta1 into project's lib folder
- 从头开始的新项目
- JDK 是 1.6.u20
- 接受默认(项目)而不是全局/模块
- 接受将 Scala 2.8.0beta1 下载到项目的 lib 文件夹中
Created a new class:
创建了一个新类:
object hello {
def main(args: Array[String]) {
println("hello: " + args);
}
}
For my efforts, I now have a brand-new error :)
为了我的努力,我现在有了一个全新的错误:)
Here it is:
这里是:
Scalac internal error: class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:202), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java:190), java.lang.ClassLoader.loadClass(ClassLoader.java:307), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301), java.lang.ClassLoader.loadClass(ClassLoader.java:248), java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72)]
Scalac 内部错误:class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:202), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java :190), java.lang.ClassLoader.loadClass(ClassLoader.java:307), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301), java.lang.ClassLoader.loadClass(ClassLoader.java:248) , java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72) ]
FINAL UPDATE
最后更新
I uninstalled 9.0.2 EA and reinstalled 9.0.1, but this time went with the 2.7.3 version of Scala rather than the default 2.7.6, because 2.7.3 is the one shown in the screen-shots at the IntelliJ website (I guess the screen-shots prove that they actually tested this version!). Now everything works!!!
我卸载了 9.0.2 EA 并重新安装了 9.0.1,但这次使用 Scala 的 2.7.3 版本而不是默认的 2.7.6,因为 2.7.3 是 IntelliJ 网站的屏幕截图中显示的版本(我猜屏幕截图证明他们确实测试了这个版本!)。现在一切正常!!!
采纳答案by Andrei Oprisan
I have encountered the same scalac error when trying to run a Scala project in Intellij Idea 9.0.2 and I've managed to find a solution by chance :). These are the steps I took in creating the project and running it.
尝试在 Intellij Idea 9.0.2 中运行 Scala 项目时遇到了相同的 scalac 错误,我设法偶然找到了解决方案:)。这些是我在创建项目和运行它时所采取的步骤。
I have created a Scala project in Intellij Idea 9.0.2 final (it was released today). I have installed the Scala plugin, restarted the IDE and created a new Scala project (with the name "TestScala") with scala-2.8.0.Beta1 as project library. Once the project is created and the scala libraries downloaded, I have created a Test.scala file with the following content:
我在 Intellij Idea 9.0.2 final(今天发布)中创建了一个 Scala 项目。我已经安装了 Scala 插件,重新启动了 IDE 并使用 scala-2.8.0.Beta1 作为项目库创建了一个新的 Scala 项目(名称为“TestScala”)。创建项目并下载 scala 库后,我创建了一个包含以下内容的 Test.scala 文件:
object Test {
def main(args:Array[String]){
println("hello")
}
}
After that, I created a launch configuration ("Edit Configurations"), choosing the "Application" template. I set as main class Test and choose the project name ("TestScala") in the "Use classpath and JDK of module" combo box. When I run the configuration I get the same error as you reported ("Scalac internal error: class java.lang.ClassNotFoundException") .
之后,我创建了一个启动配置(“编辑配置”),选择了“应用程序”模板。我设置为主类 Test 并在“使用模块的类路径和 JDK”组合框中选择项目名称(“TestScala”)。当我运行配置时,我得到与您报告的相同的错误 ("Scalac internal error: class java.lang.ClassNotFoundException") 。
Now comes the freaky part :). I right click on the project, choose "Module Settings", have a look on all settings but I don't change anything. Click "apply" and "ok", try to run configuration again and it works:) .
现在到了怪异的部分:)。我右键单击项目,选择“模块设置”,查看所有设置,但我没有更改任何内容。单击“应用”和“确定”,尝试再次运行配置,它可以工作:)。
I use Intellij Idea 9.0.2 the final release (build 95-66); Ubuntu 9.10 and JDK 1.6.0_18. I also have to mention that I had a JDK configured in Intellij, otherwise there is an extra step to configure it.
我使用 Intellij Idea 9.0.2 最终版本(构建 95-66);Ubuntu 9.10 和 JDK 1.6.0_18。还要提一下,我在Intellij中配置了一个JDK,否则配置起来还需要一个额外的步骤。
UPDATE:
更新:
When checking the setting of the module, one needs to click on the Module->Scalaand Facets->Scala (expand it and click on Scala(ProjectName)). Both of these settings are about the scala compiler and scala library location. I would guess these values are not properly set when the project is created but are saved once the user touches them and saves the settings.
当检查模块的设置,需要点击模块- >斯卡拉和Facets->斯卡拉(展开它,然后单击斯卡拉(项目名)) 。这两个设置都与 Scala 编译器和 Scala 库位置有关。我猜这些值在创建项目时没有正确设置,但是一旦用户触摸它们并保存设置就会保存。
回答by Graham Lea
To answer your question, it's difficult to get a working IDE for Scala for two reasons: (a) Scala is only just beginning to reach a wide audience and (b) due to (a), there is no business case for spending time on a Scala IDE.
为了回答您的问题,很难获得适用于 Scala 的 IDE 有两个原因:(a) Scala 才刚刚开始接触广泛的受众;(b) 由于 (a),没有商业案例可以花时间在Scala IDE。
Also, if you are old enough to cast your mind back and young enough to still remember, you would know that for the first five or more years of Java, we were stuck with okay-ish tools like JBuilder that did little more than compile your code when you said so - no error highlighting, no auto-importing, and the word refactoring didn't even exist. If you want to pioneer, you need to be prepared to cut some of the road yourself, or at least bush-bash.
此外,如果您年纪大到可以回想,年轻到还记得,那么您就会知道,在 Java 的前五年或更长时间里,我们被困在像 JBuilder 这样的好工具上,这些工具只不过是编译您的当你这么说时的代码 - 没有错误突出显示,没有自动导入,甚至不存在重构这个词。如果你想开拓,你需要准备好自己开辟一些道路,或者至少是丛林猛烈。
I know it won't help you, but I have successfully used IDEA for Scala on Linux, Mac and Windows. I typically have the Scala SDK installed somewhere locally and point IDEA at that rather than using the 'download' option.
我知道它不会帮助你,但我已经成功地在 Linux、Mac 和 Windows 上使用了 Scala 的 IDEA。我通常将 Scala SDK 安装在本地某处并将 IDEA 指向该位置,而不是使用“下载”选项。
Presently, I am mostly using an EAP version of IDEA 9on Mac OS X with Scala 2.8.0.Beta1-RC5 and it's working well (except that fsc doesn't seem to worked with mixed sources).
目前,我主要在 Mac OS X 和 Scala 2.8.0.Beta1-RC5 上使用EAP 版本的 IDEA 9,它运行良好(除了 fsc 似乎不适用于混合源)。
You could try your luck over at the IDEA Scala Plugin Discussion Forum, though I haven't had a great lot of responses to my own postings there.
您可以在IDEA Scala 插件讨论论坛上碰碰运气,尽管我在那里的帖子没有得到很多回复。
回答by Randall Schulz
Installing the plug-in is prerequisite one.
安装插件是前提条件之一。
The next thing you should do is define a library (global or project-specific; I use global) that holds the Scala library andcompiler JAR files (at a minimum, that's scala-compiler.jarand scala-library.jar). Adding source JARs and a documentation JAR or URLs is a good idea, too. Then make this library a dependency of any modules in your project that include Scala code.
您应该做的下一件事是定义一个库(全局或特定于项目;我使用全局),其中包含 Scala 库和编译器 JAR 文件(至少是scala-compiler.jar和scala-library.jar)。添加源 JAR 和文档 JAR 或 URL 也是一个好主意。然后使这个库成为项目中包含 Scala 代码的任何模块的依赖项。
Lastly, find the Scala facets in those modules and de-select both check-boxes there.
最后,在这些模块中找到 Scala 方面并取消选中那里的两个复选框。
回答by Graham Lea
I just did a fresh install and had exactly this same problem myself. It turned out that, because I had created the file in the root package, IDEA had added a package statement at the top with naming a package. I assume that this then got compiled as "package object Main" - valid syntax in 2.8? Anyway, I deleted the line that said package and it all worked fine.
我刚刚进行了全新安装,并且自己也遇到了完全相同的问题。原来,因为我在根包中创建了文件,所以IDEA在顶部添加了一个包语句,并命名了一个包。我假设这然后被编译为“包对象 Main” - 2.8 中的有效语法?无论如何,我删除了说包的那一行,一切正常。
回答by Theq
I had the same problem yesterday while trying to set it up. Solution is pretty simple, you just have to set scala somewhere in project settings.
我昨天在尝试设置时遇到了同样的问题。解决方案非常简单,您只需在项目设置中的某处设置 scala。
回答by Daniel C. Sobral
You are mixing code compiled with two different Scala versions.
您正在混合使用两个不同 Scala 版本编译的代码。
回答by Travis Stevens
I was getting this error and also had to right click on the project and "Open Module Settings". However, it was more than just hitting apply. I had to make sure that my Content Root was correct for each project. For some reason, there were some incorrect Source and Test Folders.
我收到此错误,还必须右键单击项目和“打开模块设置”。然而,这不仅仅是点击申请。我必须确保我的内容根目录对于每个项目都是正确的。出于某种原因,有一些不正确的源和测试文件夹。
My project uses maven as the main build tool and importing the project into Intellij is probably what created these incorrect settings.
我的项目使用 maven 作为主要构建工具,将项目导入 Intellij 可能是造成这些不正确设置的原因。
回答by LiorH
回答by Bruce Li
I use Netbeans to write scala programs. So far it works very well with my codes. You can try the plugin here: http://wiki.netbeans.org/Scala68v1.
我使用 Netbeans 编写 Scala 程序。到目前为止,它与我的代码配合得很好。您可以在此处试用该插件:http: //wiki.netbeans.org/Scala68v1。

