如何将 scala.sys.process 与 scala.js 一起使用

How to use scala.sys.process with scala.js

我已经将 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——虽然 语言 相同,但 环境[=19= 】 很不一样。许多标准 Scala 库根本不存在于 SJS 世界中,其中许多 不能 ,因为 JavaScript 环境的限制 运行进去了。它在语法上是合法的,所以它会编译,但在没有 Scala.js 版本的库之前它不能 运行。

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