简体   繁体   中英

How to execute system commands in scala

i am new at scala and i am trying to run this simple program that get input from the user and execute it in the operation system:

import scala.io._ 
import sys.process._

object MyCmd {
    def main(args: Array[String]) = {
      print("> ")
      var inputString = StdIn.readLine()

      while(!inputString.trim().equals("exit")) {
        var proc = stringToProcess(inputString)
        println( proc.!!)
        print("> ")
        inputString = StdIn.readLine()
     }
  }
}

but when i'm running it:

c:\IDE\scala\test>scala MyCmd
> dir
java.io.IOException: Cannot run program "dir": CreateProcess error=2, The
system cannot find the file specified
...

Any help will be much appreciate

sys.process.ProcessBuilder not runnig Windows cmd command.

See Executing shell commands from Scala REPL

If you need to use the cmd command, you can execute as

val proc = stringToProcess("cmd /C "+inputString)
println(proc.!!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM