defoo
defoo

Reputation: 5317

Start external console application from Scala in interactive mode

I am using scala.sys.process to start an external console application from within my Scala code. However, I hit a road block when the console app requires the user input.

Basically, when I start the console app with

Seq("powershell" , "myConsoleApp.exe").run

myConsoleApp.exe will not be started in its own "window". I can see the console app is running when I check the Task Manager. Without an actual window, I can't really key in anything.

I know Scala can return the program output to a String or a Stream[String] - I guess Scala will probably be able pipe input to the external process also.

But I really don't want to re-write such logic in Scala when all of them are already available in the external program.

I am wondering if there is a way to start an external console program in its own window? Or is this a shortcoming with scala.sys.process.

Thanks,

Upvotes: 2

Views: 966

Answers (3)

defoo
defoo

Reputation: 5317

After some more googling, I found that my problem is more in the way I call powershell. Here is a solution that works for me:

Seq("powershell", "Start-Process", "myConsoleApp.exe")

Upvotes: 1

idonnie
idonnie

Reputation: 1713

This will run interactively from Scala console, copy and :paste

val con = System.console
new java.lang.Thread() {
  val in = new java.lang.Thread() {
    override def run() {
      while (true) { 
        Thread.sleep(1)
        if (con.reader.ready)
          con.reader.read()        
      }       
    }
  }
  override def run() { 
    in.start()
    while (true) { 
      Thread.sleep(1000)
      con.printf("\nHai")
    }
  }
}.start()

Upvotes: 0

David Holbrook
David Holbrook

Reputation: 1617

Adapted from this Java answer Show the CMD window with Java

import scala.sys.process._   
Seq("cmd", "/c", "start", "PowerShell.exe", "myConsoleApp.exe") run

Upvotes: 1

Related Questions