如何在 IntelliJ IDEA 中运行 Scala 脚本?

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

How to run a Scala script within IntelliJ IDEA?

scalaintellij-idea

提问by javadba

Here is a trivial scala script:

这是一个简单的 Scala 脚本:

object test {
  def hi() { print("hi there from here") }
}


test.hi()

From the command line it does the expected:

它从命令行执行预期的操作:

scala /shared/scaladem/src/main/scala/test.scala
Loading /shared/scaladem/src/main/scala/test.scala...
defined module test
hi there from here
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

But within Intellij it gives a compilation error. Right click | Run test.scala

但是在 Intellij 中,它给出了编译错误。右键单击 | 运行 test.scala

expected class or object definition
test.hi()
^

BTW I also tried running as a scala worksheet. That was MUCH worse - tons of garbage output and did not even get close to compiling.

顺便说一句,我也尝试作为 Scala 工作表运行。那更糟糕 - 大量的垃圾输出,甚至没有接近编译。

Update: it appears there is an older but similar question:

更新:似乎有一个较旧但类似的问题:

How to run Scala code in Intellij Idea 10

如何在 Intellij Idea 10 中运行 Scala 代码

I went into Run Configuration and unchecked "Make" as instructed (this was bothersome but so be it ..)

我进入运行配置并按照指示取消选中“Make”(这很麻烦,但就这样吧..)

However after making that change I get a different error:

但是,在进行更改后,我收到了不同的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Either
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: scala.Either
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

Note: the Scala-Library is properly set up:

注意:Scala-Library 已正确设置:

enter image description here

enter image description here

enter image description here

enter image description here

Another update (after @lhuang's comment below)I followed the suggestion to create another project from scratch. In that case a Scala Worksheet worked properly (test.sc). But a scala script (which works when running command line via "scala test.scala" ) still does notwork, even in this brand new "scala" project.

另一个更新(在下面@lhuang 的评论之后)我按照建议从头开始创建另一个项目。在这种情况下,Scala 工作表可以正常工作(test.sc)。但Scala的脚本(通过运行“斯卡拉test.scala”命令行时有效)仍然没有没有工作,即使在这种全新的“阶”项目。

采纳答案by javadba

The answer here is a combination of items:

这里的答案是项目的组合:

  • (a) create a brand new Scala project (as suggested by @lhuang) and
  • (b) when running a script, you need to go into the Run Configuration and remove the Make step (as mentioned in the 'related' SOF question).
  • (a) 创建一个全新的 Scala 项目(如@lhuang 所建议的)和
  • (b) 运行脚本时,您需要进入 Run Configuration 并删除 Make 步骤(如“相关”SOF 问题中所述)。

This shows rough edges with Intellij and its scala plugin. Especially when I want to integrate scala with java it is apparently difficult if even possible using Intellij at this time (need to create new Scala project on a frequent basis is a non-starter for mostly java projects attempting to incorporate scala).

这显示了 Intellij 及其 scala 插件的粗糙边缘。特别是当我想将 scala 与 java 集成时,如果此时甚至可能使用 Intellij 显然很困难(需要经常创建新的 Scala 项目对于大多数尝试合并 scala 的 java 项目来说是不可行的)。

But for scala-first projects it seems this may be workable.

但是对于 Scala 优先的项目,这似乎是可行的。

回答by Kai Mechel

What works for me, is:

对我有用的是:

  1. Write a Scala script, e.g. MyScript.scala
  2. In the menu select: Run -> Edit Configurations...
  3. Press the "+" (?N also works on the Mac in this dialog)
  4. Select "Scala Script"
  5. Then select your Script file in this dialog enter image description here
  1. 编写 Scala 脚本,例如 MyScript.scala
  2. 在菜单中选择:运行 -> 编辑配置...
  3. 按“+”(?N 在此对话框中也适用于 Mac)
  4. 选择“Scala脚本”
  5. 然后在此对话框中选择您的脚本文件 enter image description here

Now you can run your script. It's a bit inconvenient, but it works.

现在您可以运行您的脚本。这有点不方便,但它有效。

Note: This works for me in IntelliJ IDEA 14.x

注意:这在 IntelliJ IDEA 14.x 中对我有用

回答by guzial

Your code worksin command line because it is "script", when you want to make it runnable in project there are ways to do it:

您的代码在命令行中工作,因为它是“脚本”,当您想让它在项目中运行时,有几种方法可以做到

  1. By creating object which extends App

    object test {
        def hi() { print("hi there from here") }
    }
    
    object runnable extends App {
        test.hi()
    }
    
  2. or java-like solution i.e. creating main method

    object test {
        def hi() { print("hi there from here") }
    }
    
    object runnable {
        def main(args: Array[String]) {
            test.hi()
        }
    }
    
  3. when need to execute as script - I do it like that: select code to execute by mouse, then choose from context menu "Send Selection To Scala Console"

  1. 通过创建扩展 App 的对象

    object test {
        def hi() { print("hi there from here") }
    }
    
    object runnable extends App {
        test.hi()
    }
    
  2. 或类似 java 的解决方案,即创建 main 方法

    object test {
        def hi() { print("hi there from here") }
    }
    
    object runnable {
        def main(args: Array[String]) {
            test.hi()
        }
    }
    
  3. 当需要作为脚本执行时 - 我这样做:选择要通过鼠标执行的代码,然后从上下文菜单中选择“将选择发送到 Scala 控制台”

回答by Jana

There are 2 things to understand first. Scala works as both interpreter and compiler. You are trying to with interpreter "scala test.scala" without using "scalac test.scala" and interpreter works without main method also.

首先要了解两件事。Scala 既是解释器又是编译器。您正在尝试使用解释器“scala test.scala”而不使用“scalac test.scala”,并且解释器也可以在没有主要方法的情况下工作。

Other way you can do in intellij is open the terminal or command prompt in intellij and run scala test.scala from the file location (Pre-requisite is scala in present the system path)

您可以在 intellij 中执行的其他方法是在 intellij 中打开终端或命令提示符并从文件位置运行 scala test.scala (先决条件是当前系统路径中的 scala)