Prompt user from command line


(Dominik Yakoubek) #1

I’m currently working on a program that takes information form google sheets and runs calculations. I use “gradle -q run” to run my program and I want it to prompt the user inside the terminal. Right now I am using scanner but it says “Enter your name: Exception in thread “main” java.util.NoSuchElementException” when I run instead of waiting for me to add a input, How do I fix this?


(Raleigh) #2

The run task from the Application plugin is of type JavaExec
see The Application Plugin

If you look at the docs for JavaExec you’ll see the problem is with the default value for stdIn (emphasis mine)

standardInput | The standard input stream for the process executing the command. The stream is closed after the process
completes. Defaults to an empty stream.

Therefore you need to hookup standard in to System.in

something like this in your build.gradle should work:

run {
    standardInput = System.in
}