【问题标题】:How to use scala.sys.process with scala.js如何在 scala.js 中使用 scala.sys.process
【发布时间】:2017-08-02 16:50:57
【问题描述】:

我已将 scala.sys.process._ 导入到我的 scala.js 项目中。仅此一项不会导致任何问题,但是如果我添加一个简单的命令,例如println("ls".!!),我会收到大量错误,例如

[error] Referring to non-existent class java.lang.ProcessBuilder
[error]   called from scala.sys.process.ProcessCreation.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.Process$.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.ProcessCreation.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.Process$.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.ProcessCreation.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.Process$.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.ProcessImplicits.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.package$.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from draw.Main$.main(org.scalajs.dom.raw.HTMLCanvasElement)scala.Unit
[error]   called from draw.Main$.$$js$exported$meth$main(org.scalajs.dom.raw.HTMLCanvasElement)java.lang.Object
[error]   called from draw.Main$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   scala.sys.process.Process$
[error]   scala.sys.process.package$
[error]   draw.Main$
[error] Referring to non-existent class java.io.File
[error]   called from scala.sys.process.ProcessCreation.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.Process$.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.ProcessCreation.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.Process$.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.ProcessCreation.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.Process$.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.ProcessImplicits.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from scala.sys.process.package$.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error]   called from draw.Main$.main(org.scalajs.dom.raw.HTMLCanvasElement)scala.Unit
[error]   called from draw.Main$.$$js$exported$meth$main(org.scalajs.dom.raw.HTMLCanvasElement)java.lang.Object
[error]   called from draw.Main$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   scala.sys.process.Process$
[error]   scala.sys.process.package$
[error]   draw.Main$
[error] Referring to non-existent method java.lang.ProcessBuilder.environment()java.util.Map

导入 java.lang.ProcessBuilder 和 java.io.File 等额外的类对这些错误的内容没有影响。我在这里缺少一些非常简单的东西吗?

谢谢!

【问题讨论】:

    标签: scala scala.js


    【解决方案1】:

    您不能简单地将任意 Scala 库导入到 Scala.js 中——虽然 语言 是相同的,但 环境 却大不相同。许多标准 Scala 库在 SJS 世界中根本不存在,其中许多不能,因为它运行的 JavaScript 环境的限制。它在语法上是合法的,所以它会编译,但在没有 Scala.js 版本的库之前它无法运行。

    总的来说,您应该假设像这样的库存在于 SJS 世界中,除非您发现有人专门移植了它。 (老实说,我不知道是否有人为 Node.js 移植了 scala.sys.process;这在浏览器环境中没有多大意义……)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-29
      • 2017-04-10
      • 1970-01-01
      • 2021-12-31
      • 2015-07-15
      • 2017-10-12
      • 1970-01-01
      • 2016-05-09
      相关资源
      最近更新 更多