如何在 Intellij Idea 11 上运行 Scala 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9563342/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:57:27  来源:igfitidea点击:

How to run scala code on Intellij Idea 11?

scalaintellij-idea

提问by rivendell

I am new to intellij idea and I decided to shift because I found the scala plugin on eclipse to be annoyingly buggy. But, intellij idea, which I have heard to be very good, looks difficult to use. I looked through the getting started with scala plugin pagebut couldn't find documentation specific to the latest version of lightweight ide of intellij idea 11 for scala.

我是 intellij 想法的新手,我决定转移,因为我发现 eclipse 上的 scala 插件令人讨厌。但是,我听说非常好的 Intellij 想法看起来很难使用。我浏览了scala 插件入门页面,但找不到特定于最新版本的 Intellij idea 11 for Scala 轻量级 ide 的文档。

I have created a project and made a hello world object in scala but I am not able to run it. On trying to run it, it shows the scala interpreter (type in expressions to get them evaluated). I don't exactly understand what changes to make in the project structure to be able to run scala code. As of now, i added the jsdk to point to my $JAVA_HOME and the scala library is also added. The scala plugin is also working in that syntax highlighting etc is fine.

我创建了一个项目并在 Scala 中创建了一个 hello world 对象,但我无法运行它。在尝试运行它时,它会显示 Scala 解释器(输入表达式以对其进行评估)。我不完全明白要在项目结构中进行哪些更改才能运行 Scala 代码。到目前为止,我添加了 jsdk 以指向我的 $JAVA_HOME,并且还添加了 Scala 库。scala 插件也在语法高亮等方面工作很好。

The intellij idea user interface is also not very friendly and I don't get how to run the code. What modules am I supposed to add? Also, does compiled scala code run with jvm? I'll be grateful to anyone who helps out.. I have been putting off writing scala code because I am not able to find THE IDE.

intellij idea 的用户界面也不是很友好,我不知道如何运行代码。我应该添加哪些模块?另外,编译的 Scala 代码是否与 jvm 一起运行?我会感谢任何提供帮助的人......我一直在推迟编写 Scala 代码,因为我找不到 IDE。

EDIT: Thanks for all the help! I am now able to run a basic scala program. I am trying to add external jars. What is the equivalent of doing Project right click-> Configure Build Path -> adding external jar files in eclipse? I tried adding "dependencies " under modules tab in project structure. It's adding the jar files but somehow the classes in the jar files are not still recognised.

编辑:感谢所有的帮助!我现在可以运行一个基本的 Scala 程序。我正在尝试添加外部罐子。相当于在 eclipse 中执行 Project 右键单击​​-> 配置构建路径-> 添加外部 jar 文件?我尝试在项目结构的模块选项卡下添加“依赖项”。它正在添加 jar 文件,但不知何故仍然无法识别 jar 文件中的类。

回答by UberMouse

With the Scala plugin installed either

安装了 Scala 插件

  • A. Create a new project and select the Scala facet in the creation phase. It will create the Scala library lib and Scala compiler lib and setup the facet for you

  • B. If you already have a project. Go to Project Structure -> Modules and right click the module and go Add facet and add a Scala facet. Now you need to add scala-library.jar as a library of the module and go into the Scala facet and point it to a library containing scala-compiler.jar

  • A.新建一个项目,在创建阶段选择Scala facet。它将创建 Scala 库库和 Scala 编译器库并为您设置构面

  • B. 如果您已经有一个项目。转到 Project Structure -> Modules 并右键单击模块并转到 Add facet 并添加一个 Scala facet。现在您需要将 scala-library.jar 添加为模块的库,并进入 Scala facet 并将其指向包含 scala-compiler.jar 的库

Some more information

更多信息

enter image description here

在此处输入图片说明

this is what your module should look like under project settings

这就是你的模块在项目设置下的样子

enter image description here

在此处输入图片说明

select the Scala facet and this is what you should see (Library name for the compiler is unimportant as long as it says (version xxx) next to the library name

选择 Scala 方面,这就是您应该看到的(编译器的库名称并不重要,只要它在库名称旁边显示(版本 xxx)

enter image description here

在此处输入图片说明

these are the jar files in my scala-compiler lib

这些是我的 Scala 编译器库中的 jar 文件

enter image description here

在此处输入图片说明

and these are the jar files in my scala-library lib

这些是我的 scala-library 库中的 jar 文件

With everything setup like that you should be able to right click -> new Scala class (Select object from the dropdown) in a source directory and add

完成所有设置后,您应该能够在源目录中右键单击 -> 新 Scala 类(从下拉列表中选择对象)并添加

def main(args:Array[String]) {
   println("Hello world")
}

then right click on the class and select Run Classnam.main() and IntelliJ will setup a run configuration for you.

然后右键单击该类并选择 Run Classnam.main(),IntelliJ 将为您设置运行配置。

回答by Jens Egholm

Edit: I can see someone posted somewhat the same I want to say here. I hope this can give a few more in-depths hints, so I will post it anyway :-)

编辑:我可以看到有人发帖与我想在这里说的有些相同。我希望这可以提供一些更深入的提示,所以无论如何我都会发布它:-)

If you would like to run Scala native in IntelliJ (I shifted to IntelliJ and still can't get my hands down) you need to make sure a few things are in order. Most of this happens under File -> Project Structure which requires you to create a project (when you get a hang of it, it is much more logical than Eclipse -- and Netbeans for that matter). I am also assuming you have selected a jdk, but this can also be done in the Project Structure.

如果您想在 IntelliJ 中运行原生 Scala(我转向 IntelliJ 但仍然无法放下手),您需要确保一些事情是有序的。大多数情况发生在 File -> Project Structure 下,这需要您创建一个项目(当您掌握它时,它比 Eclipse 和 Netbeans 更合乎逻辑)。我还假设您选择了 jdk,但这也可以在项目结构中完成。

Now, to Scala:

现在,到 Scala:

  1. Make sure you have the Scala plugin. Visit the Settings (File -> Settings; can also be found at the start-up screen) and locate 'Plugins' (for me it's nr. 8 from the bottom). Under "Browse Repositories" you should be able to search for "scala", find and install a plugin. 1 A. If no plugins pops up you are probably using a version of intellij where scala is not (yet) supported. If that happens you can go to the homepage for the pluginand download the nightly build.
  2. Import Scala as a library in File -> Project Structure -> Global Libraries. Click the plus sign, select Java (Scala is a Java-lib), browse to your scala-dir (which can be downloaded here: http://plugins.intellij.net/plugin/?id=1347) and select the libdirectory. Press ok and you should see the library popping up on the list.
  3. Make sure the facet(Scala framework) is in place. Goto File -> Project Structure -> Facets. Click on the plus-sign and find Scala. A sublink should appear (if not double-click the Scala link). Clicking the sublink make sure you have selected a compiler before continuing. This should be provided by the modules selected before. I normally use FSC (Fast Scala Compiler) but it's probably a good idea to test the basic settings before experimenting.
  4. Make sure the Scala Module is in place. In Project Structure -> Modules click on the module you want to compile in scala and click the plus icon above and select Scala. Again be sure to select a compiler library.
  5. If you want to, setup configurations for the compiler under the facet
  1. 确保你有 Scala 插件。访问设置(文件 -> 设置;也可以在启动屏幕中找到)并找到“插件”(对我来说,它是底部的第 8 个)。在“浏览存储库”下,您应该能够搜索“scala”,找到并安装插件。1 A. 如果没有插件弹出,您可能使用的是 Intellij 版本,其中(尚)不支持 Scala。如果发生这种情况,您可以转到插件的主页并下载每晚构建。
  2. 在 File -> Project Structure -> Global Libraries 中将 Scala 作为库导入。单击加号,选择 Java(Scala 是 Java-lib),浏览到您的 scala-dir(可在此处下载:http: //plugins.intellij.net/plugin/?id=1347)并选择lib目录。按确定,您应该会看到该库出现在列表中。
  3. 确保facet(Scala 框架)到位。转到文件 -> 项目结构 -> 构面。单击加号并找到 Scala。应该会出现一个子链接(如果没有,请双击 Scala 链接)。单击子链接确保您在继续之前选择了编译器。这应该由之前选择的模块提供。我通常使用 FSC(快速 Scala 编译器),但在试验之前测试基本设置可能是个好主意。
  4. 确保 Scala 模块就位​​。在 Project Structure -> Modules 中单击要在 Scala 中编译的模块,然后单击上面的加号图标并选择 Scala。再次确保选择编译器库。
  5. 如果需要,请在 facet 下为编译器设置配置

回答by Lothar Krause

A good walk-through on how to set up a Scala project in Intellij is located here: http://sonyarouje.com/2011/03/18/running-scala-in-intellij-idea-10/

关于如何在 Intellij 中设置 Scala 项目的一个很好的演练位于:http://sonyarouje.com/2011/03/18/running-scala-in-intellij-idea-10/

回答by Khurram Ijaz

Use sbt with sbt-idea plugin to generate idea project files for complex projects.

使用 sbt 和 sbt-idea 插件为复杂项目生成创意项目文件。

Or

或者

For simple projects.

对于简单的项目。

Add scala nature to the project, and you can right click and run any file extending App.

将scala性质添加到项目中,您可以右键单击并运行任何扩展App.js的文件。