Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Stdin.readLine() does not seem to work as expected

I am trying to write a simple console client application where i can present some options to the user, get their input and act accordingly. If i run the code through intellij or paste it into the scala console, it works. If i run it through sbt (which is how i really need it to run), i run into all sorts of problems.

I have sbt version 0.13.8, OS is Mac, my build.sbt contains:

scalaVersion := "2.11.6"
fork in run := true

EDIT I started with the minimum scala activator template in case that is useful info in this context

I have simplified the code to barebones,

import scala.io.StdIn._

object TestClient {
  def main(args: Array[String]): Unit = {
    join()
  }

  def join(): Unit = {
    val name = readLine(s"Enter your name.${System.getProperty("line.separator")}")
    name match {
      case n: String => println(n)
      case o => {
        println(s"invalid name ${o}")
        join()
      };
    }
  }
}

When go into sbt and from the prompt enter run. one of the following seems to happen

1) I get this exception as soon as i run

Exception in thread "Thread-2" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:3332)
        at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
        at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
        at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:569)
        at java.lang.StringBuffer.append(StringBuffer.java:369)
        at java.io.BufferedReader.readLine(BufferedReader.java:370)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at sbt.BasicIO$$anonfun$processFully$1$$anonfun$apply$8.apply(ProcessImpl.scala:58)
        at sbt.BasicIO$$anonfun$processFully$1$$anonfun$apply$8.apply(ProcessImpl.scala:58)
        at sbt.BasicIO$.readFully$1(ProcessImpl.scala:63)
        at sbt.BasicIO$.processLinesFully(ProcessImpl.scala:69)
        at sbt.BasicIO$$anonfun$processFully$1.apply(ProcessImpl.scala:58)
        at sbt.BasicIO$$anonfun$processFully$1.apply(ProcessImpl.scala:55)
        at sbt.SimpleProcessBuilder$$anonfun$3.apply$mcV$sp(ProcessImpl.scala:354)
        at sbt.Spawn$$anon$3.run(ProcessImpl.scala:17)

2) No memory issues, but getting these messages in an infinite loop, so the readline does not seem to be waiting for any input

background log: info: Enter your name.
background log: info: invalid name null
background log: info: Enter your name.
background log: info: invalid name null
...

Either way I am unable to actually enter any input in the console. Not sure what I'm missing or doing wrong

like image 489
Omar A Avatar asked Apr 09 '15 12:04

Omar A


1 Answers

Put

connectInput in run := true

in your build.sbt. See the official docs for more info on how to correctly handle forks in sbt.

like image 66
kiritsuku Avatar answered Oct 27 '22 00:10

kiritsuku