如何在scala.js中使用scala.sys.process

时间:2017-03-12 17:21:44

标签: scala 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)对这些错误的内容没有影响。我在这里缺少一些非常简单的东西吗?

谢谢!

1 个答案:

答案 0 :(得分:7)

您不能简单地将任意Scala库导入Scala.js - 而语言是相同的,环境是非常不同的。许多标准的Scala库根本不存在于SJS世界中,并且其中许多不能,因为它运行的JavaScript环境存在局限性。它是语法合法,所以它会编译,但它不能在没有scala.js版本的库之前运行。

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