在Windows上的Scala代码中运行shell命令似乎需要命令的完整绝对路径

Cui*_*崔鹏飞 7 windows bash shell powershell scala

当我尝试在Mac上运行shell命令时,它按预期工作如下:

scala> import scala.sys.process._
import scala.sys.process._

scala> """protractor --version"""!
warning: there were 1 feature warning(s); re-run with -feature for details
Version 0.24.0
res12: Int = 0

scala>
Run Code Online (Sandbox Code Playgroud)

但如果我在Windows上这样做,我会得到这个:

scala> import scala.sys.process._
import scala.sys.process._

scala> """protractor --version"""!
warning: there were 1 feature warning(s); re-run with -feature for details
java.io.IOException: Cannot run program "protractor": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
Run Code Online (Sandbox Code Playgroud)

好像我必须在Windows上这样做:

scala> import scala.sys.process._
scala> """C:\Users\twer\AppData\Roaming\npm\protractor.cmd --version"""!
warning: there were 1 feature warning(s); re-run with -feature for details
Version 0.24.0
res11: Int = 0

scala>
Run Code Online (Sandbox Code Playgroud)

我必须提供命令的完整绝对路径.

但我确信该命令在路径中可用.

反正有没有避免这个?

dan*_*xon 9

你可以试试这个:

val command = Seq("protractor", "--version")
val os = sys.props("os.name").toLowerCase
val panderToWindows = os match {
  case x if x contains "windows" => Seq("cmd", "/C") ++ command
  case _ => command
}
panderToWindows.!
Run Code Online (Sandbox Code Playgroud)