Scala 2.11.2 ScriptEngine抛出错误

时间:2014-07-30 17:05:35

标签: scala intellij-idea scriptengine scala-2.11

我正在尝试在InteliJ IDEA Scala工作表中运行Scala ScriptEngine(Scala 2.11.2)

下一个代码:

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中运行它?

2 个答案:

答案 0 :(得分:3)

看起来,由于IntelliJ的启动方式与sbt不同,我假设你的代码正在运行,你需要使用类似下面的示例。唯一的区别是usejavacp行与IntelliJ如何启动java / scala项目相比,而sbt就是使用classLoader magic在同一个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的原因是它将classLoader带入范围。

参考上一个解决方案:https://stackoverflow.com/a/24351657/1951691

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

答案 1 :(得分:0)

我必须将Scala编译器包含在Project Structure中Intellij内的全球图书馆

enter image description here