如何在IntelliJ IDEA中使用Scala(或:为什么很难获得适用于Scala的IDE)? [英] How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

查看:470
本文介绍了如何在IntelliJ IDEA中使用Scala(或:为什么很难获得适用于Scala的IDE)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近放弃了在Eclipse中使用Scala的尝试(完成之类的基本操作不起作用).所以现在我正在尝试IntelliJ.我走的不远.

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.

我已经能够编辑程序(在语法高亮显示和完成中...是的!).但是我什至无法运行最简单的"Hello World".这是原始错误:

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:


Scala signature Predef has wrong version
Expected 5.0
found: 4.1 in .... scala-library.jar

但是那是昨天发布的IDEA 9.0.1.见下文...

But that was yesterday with IDEA 9.0.1. See below...

更新

今天,我使用4/14稳定版的Scala插件卸载了IntelliJ 9.0.1,并安装了9.0.2 Early Availability.

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.

然后我通过向导从头开始设置项目:

Then I setup a project from scratch through the wizards:

  • 从头开始新建项目
  • JDK是1.6.u20
  • 接受默认的(项目)而不是全局的/模块
  • 接受将Scala 2.8.0beta1下载到项目的lib文件夹中

创建了一个新类:


object hello {
  def main(args: Array[String]) {
    println("hello: " + args);
  }
}

为了我的努力,我现在遇到了一个全新的错误:)

For my efforts, I now have a brand-new error :)

这里是:

Scalac内部错误:类java.lang.ClassNotFoundException [java.net.URLClassLoader $ 1.run(URLClassLoader.java:202),java.security.AccessController.doPrivileged(本机方法),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(本机方法),java.lang.Class.forName(Class.java:169),org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java :72)]

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)]

最终更新

我卸载了9.0.2 EA,然后重新安装了9.0.1,但这一次是使用2.7.3版本的Scala而不是默认的2.7.6,因为2.7.3是屏幕截图中显示的版本. IntelliJ网站(我猜这些屏幕截图证明了他们实际上已经测试过该版本!).现在一切正常!

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!!!

推荐答案

在Intellij Idea 9.0.2中尝试运行Scala项目时遇到了相同的scalac错误,并且设法找到了解决方法:) .这些是我创建和运行项目的步骤.

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 final(今天发布)中创建了一个Scala项目.我已经安装了Scala插件,重新启动了IDE,并使用scala-2.8.0.Beta1作为项目​​库创建了一个新的Scala项目(名称为"TestScala").创建项目并下载Scala库后,我创建了一个Test.scala文件,其内容如下:

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:

object Test  {
 def main(args:Array[String]){
   println("hello")
 }
}

此后,我选择了应用程序"模板来创建启动配置(编辑配置").我将主类设置为Test,然后在使用模块的类路径和JDK"组合框中选择项目名称("TestScala").运行配置时,出现与您报告的错误相同的错误("Scalac内部错误:类java.lang.ClassNotFoundException").

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") .

现在出现了怪异的部分:).我右键单击该项目,选择模块设置",查看所有设置,但我什么都没有改变.单击应用"和确定",尝试再次运行配置,并可以使用:).

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 :) .

我使用的是Intellij Idea 9.0.2最终版本(内部版本95-66); Ubuntu 9.10和JDK 1.6.0_18.我还要提到的是,我在Intellij中配置了JDK,否则还有一个额外的步骤来配置它.

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.

更新:

在检查模块设置时,需要单击 Module-> Scala Facets-> Scala(展开并单击Scala(ProjectName)).这两个设置都与scala编译器和scala库位置有关.我猜这些值在创建项目时没有正确设置,但是一旦用户触摸它们并保存设置便会保存下来.

When checking the setting of the module, one needs to click on the Module->Scala and 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.

这篇关于如何在IntelliJ IDEA中使用Scala(或:为什么很难获得适用于Scala的IDE)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆