简体   繁体   English

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

[英]How to run a Scala script within IntelliJ IDEA?

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. 但是在Intellij中它会产生编译错误。 Right click | 右键单击| Run test.scala 运行test.scala

expected class or object definition
test.hi()
^

BTW I also tried running as a scala worksheet. BTW我也尝试过运行scala工作表。 That was MUCH worse - tons of garbage output and did not even get close to compiling. 这是更糟糕的 - 大量的垃圾输出,甚至没有接近编译。

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$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.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已正确设置:

在此输入图像描述

在此输入图像描述

Another update (after @lhuang's comment below) I followed the suggestion to create another project from scratch. 另一个更新(在@ lhuang的评论之后)我按照建议从头开始创建另一个项目。 In that case a Scala Worksheet worked properly (test.sc). 在这种情况下,Scala工作表正常工作(test.sc)。 But a scala script (which works when running command line via "scala test.scala" ) still does not work, even in this brand new "scala" project. 但是scala脚本(通过“scala test.scala”运行命令行时仍然有效)仍然无效 ,即使在这个全新的“scala”项目中也是如此。

The answer here is a combination of items: 这里的答案是项目的组合:

  • (a) create a brand new Scala project (as suggested by @lhuang) and (a)创建一个全新的Scala项目(由@lhuang建议)和
  • (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). (b)在运行脚本时,您需要进入运行配置并删除Make步骤(如'相关'SOF问题中所述)。

This shows rough edges with Intellij and its scala plugin. 这显示了Intellij及其scala插件的粗糙边缘。 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). 特别是当我想将scala与java集成时,如果甚至可能在此时使用Intellij显然很困难(需要经常创建新的Scala项目对于大多数试图合并scala的java项目来说都是非首发)。

But for scala-first projects it seems this may be workable. 但对于scala-first项目来说,似乎这可能是可行的。

What works for me, is: 对我有用的是:

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

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

Your code works in 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 通过创建扩展App的对象

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

     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" 当需要作为脚本执行时 - 我这样做:选择要通过鼠标执行的代码,然后从上下文菜单中选择“将选择发送到Scala控制台”

There are 2 things to understand first. 首先要理解两件事。 Scala works as both interpreter and compiler. Scala既是解释器又是编译器。 You are trying to with interpreter "scala test.scala" without using "scalac test.scala" and interpreter works without main method also. 您正在尝试使用解释器“scala test.scala”而不使用“scalac test.scala”,并且解释器也可以在没有main方法的情况下工作。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM