Why doesn't System.in.read() block when I'm using Gradle?

When I’m doing client / server development, I find it useful to use System.in.read() to wait for user input. For example:

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("Press enter key to stop.");
     System.in.read();
    }
}

When I run the above from my IDE (IntelliJ IDEA), it blocks. However, when I run it with Gradle’s application plugin, the process exits immediately. I’m not sure why. Any ideas?

I’ve seen GRADLE-1147 and GRADLE-1168, but they seem to be related to getting input for build scripts.

By default the ‘run’ task (and tasks of type ‘JavaExec’ in general) default to using an empty stdin for child processes. If you want the child process to consume from System.in, you can configure the run task like this:

run {

standardInput = System.in

}

5 Likes