1

我正在尝试在 InteliJ IDEA Scala Worksheet (Scala 2.11.2) 中运行 Scala ScriptEngine

下一个代码:

import javax.script.ScriptEngineManager
val e = (new ScriptEngineManager()).getEngineByName("scala")
e.eval("1 to 10 foreach println")

抛出错误:

e: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@49049a04
[init] error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by C:\Program Files\Java\jdk1.8.0_11\jre\lib\rt.jar(java/lang/Object.class)

Failed to initialize compiler: object scala in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programatically, settings.usejavacp.value = true.
scala.reflect.internal.MissingRequirementError: object scala in compiler mirror not found.
    at scala.reflect.internal.MissingRequirementError$.signal(D:/workspace/Poster/src/test.sc:13)
    at scala.reflect.internal.MissingRequirementError$.notFound(D:/workspace/Poster/src/test.sc:14)
    at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(D:/workspace/Poster/src/test.sc:49)
    at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(D:/workspace/Poster/src/test.sc:62)
    at scala.reflect.internal.Mirrors$RootsBase.getPackage(D:/workspace/Poster/src/test.sc:169)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage$lzycompute(D:/workspace/Poster/src/test.sc:157)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage(D:/workspace/Poster/src/test.sc:157)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass$lzycompute(D:/workspace/Poster/src/test.sc:158)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass(D:/workspace/Poster/src/test.sc:158)
    at scala.reflect.internal.Definitions$DefinitionsClass.init(D:/workspace/Poster/src/test.sc:1373)
    at scala.tools.nsc.Global$Run.<init>(D:/workspace/Poster/src/test.sc:1225)
    at scala.tools.nsc.interpreter.IMain.compileSourcesKeepingRun(D:/workspace/Poster/src/test.sc:384)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compileAndSaveRun(D:/workspace/Poster/src/test.sc:803)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(D:/workspace/Poster/src/test.sc:762)
    at scala.tools.nsc.interpreter.IMain.bind(D:/workspace/Poster/src/test.sc:626)
    at scala.tools.nsc.interpreter.IMain.bind(D:/workspace/Poster/src/test.sc:663)
    at scala.tools.nsc.interpreter.IMain$$anonfun$quietBind$1.apply(D:/workspace/Poster/src/test.sc:662)
    at scala.tools.nsc.interpreter.IMain$$anonfun$quietBind$1.apply(D:/workspace/Poster/src/test.sc:662)
    at #worksheet#.#worksheet#(D:/workspace/Poster/src/test.sc:200)

build.sbt 如下:

name := "Poster"

version := "1.0"

libraryDependencies += "org.seleniumhq.selenium" % "selenium-java" % "2.42.2"

libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.11.2"

libraryDependencies += "org.scala-lang" % "scala-library" % "2.11.2"

包括所有需要的依赖项,我不明白为什么它不起作用。

Eclipse Luna + Eclipse IDE 4 中的相同项目工作正常!

如何在 InteliJ IDEA 中运行它?

4

2 回答 2

3

看起来由于 IntelliJ 的启动方式与 sbt 不同,我假设您的代码正在运行,因此您需要使用类似于下面示例的内容。唯一的区别是 usejavacp 行,它与 IntelliJ 如何启动 java/scala 项目有关,而 sbt 使用 classLoader 魔法在同一个 vm 中启动它们。

  val engine = new ScriptEngineManager().getEngineByName("scala")
  val settings = engine.asInstanceOf[scala.tools.nsc.interpreter.IMain].settings
  settings.embeddedDefaults[YourClass]
  settings.usejavacp.value = true

EmbeddedDefaults 的原因是将类加载器带入范围。

参考之前的解决方案:https ://stackoverflow.com/a/24351657/1951691

这个设置适用于我作为测试运行,我没有使用工作表,但假设启动机制是相同的。

于 2014-09-17T06:07:26.280 回答
0

我必须在项目结构中包含 Scala 编译器 | Intellij 中的全局库

在此处输入图像描述

于 2014-11-12T04:36:12.687 回答