0

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

1
  • I can't reproduce this. Copy pasting this code works for me. Which Scala version are you using? Commented Aug 7, 2016 at 12:19

1 Answer 1

3

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.!!)
Sign up to request clarification or add additional context in comments.

1 Comment

I've looked in many articles, none of them wrote anything about cmd /C nor stringToProcess that helped a lot. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.