2014-06-20 1 views
6

Quando si tenta di eseguire comandi di shell su Mac, ha funzionato come previsto in questo modo:eseguire comandi di shell nel codice Scala su Windows sembra richiedere il percorso assoluto completo del comando

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> 

Ma se lo faccio su Windows, ottengo questo:

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) 

mi sembra di avere a che fare in questo modo su 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> 

devo fornire t è il percorso assoluto completo del comando.

Ma sono certo che il comando è disponibile nel percorso.

C'è comunque da evitare?

risposta

9

Si potrebbe provare questo:

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.!