Yaron
Yaron

Reputation: 1

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

Upvotes: 0

Views: 2482

Answers (1)

KosWarm
KosWarm

Reputation: 626

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

Upvotes: 3

Related Questions