我想编译一下:
import scala.sys.process._
val output = "scala".!!
但是告诉我这个:
java.io.IOException:无法运行程序“scala”:CreateProcess error = 2,系统找不到指定的文件
这也行不通:
val cmd = "\"C:\\Program Files (x86)\\scala\\bin\\scalac.bat\\\""
val output = "cmd".!!
我的环境变量也很好。 (对于java:C:\ Program Files \ Java \ jdk1.8.0_111 \和scala: C:\ Program Files(x86)\ scala 和路径变量:%JAVA_HOME%\ bin < / strong>和%SCALA_HOME%\ bin
在cmd工作中键入“scala”。 这段代码也有效:
import scala.sys.process._
val output = "java".!!
Windows 10
答案 0 :(得分:1)
命令scala
是一个带有交互式控制台的REPL。您是否尝试从交互式控制台运行交互式命令?
它应该适用于任何非交互式命令,例如ls -l
(dindows中的DIR
)或date
,如下面的示例(在unix中),
scala> val output = "date".!!
output: String =
"Mon May 15 14:52:54 PDT 2017
"
或
scala> val output = "java -version".!!
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
output: String = ""
或,
scala> Seq("echo", "urayagppd") #>> new File("mylogs.log")
res2: scala.sys.process.ProcessBuilder = ( [echo, urayagppd] #| /Users/prayagupd/myrepo/mylogs.log )
但要运行某些互动过程,例如ssh或其他内容,
scala> val scalaProcess = Process("""scala""")
scalaProcess: scala.sys.process.ProcessBuilder = [scala]
scala> val exitCode = scalaProcess.!
Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> :quit
exitCode: Int = 1
scala> val p = scalaProcess.run
p: scala.sys.process.Process = scala.sys.process.ProcessImpl$SimpleProcess@af9dd34
scala> Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> :quit
scala> val exitCode = p.exitValue
exitCode: Int = 1