I’m trying to use `readLine()` to read user input....
# announcements
c
I’m trying to use
readLine()
to read user input. It is working fine when I run from Android Studio, although when I run from CLI with
./gradlew run
it seems to not be blocking to wait for
readLine()
, and is immediately returning
null
. Is there something I can do to achieve the expected behavior from CLI?
d
i don’t know if this is the solution, but searching for “gradle run stdin” got me this: https://discuss.gradle.org/t/why-doesnt-system-in-read-block-when-im-using-gradle/3308/2 — i think “stdin” is the key term to use here.
and it makes sense — if i type something while gradle is running, whatever i’m typing doesn’t make it to the shell after it exits. it’s consumed by gradle.
c
Hmm okay that did solve blocking for STD in now. Now it’s awkwardly showing
Enter: =====--> 85% EXECUTING [10s]
all the time, and not actually displaying what I’m typing 😕
d
i’m out of my depth at this point. fwiw, when i’ve written command line stuff, i’ve built it using gradle and then ran it using
java -jar ...
. awkward but bash aliases can help there.
c
Awesome that is exactly what I’m looking for, thanks @dpk
👍 1
Btw I was able to get it to work with
./gradlew run --console plain
, and
Copy code
apply plugin: 'java'
apply plugin: 'application'

run {
    standardInput = <http://System.in|System.in>
    mainClassName = "com.mycompany.MainKt"
}
--console plain
made it so that the gradle pretty text didn’t disrupt input
And the application plugin with the
standardInput
option made it so that
readLine()
wouldn’t immediately quit
And the application plugin with the
mainClassName
option paired up the main class with the gradle run task
d
awesome. i’ll keep that in mind next time i have to create a command line app (probably to test one of the non-android modules in my android app).