4

I'm using Gradle 2.13 with Java 1.8.0_121.

One of our Gradle tasks relies on user input.

def user = System.console().readLine('Please enter new user username: ')

However I get the following error: > Cannot invoke method readLine() on null object

So console() must be null... okay. I found this related issue suggesting to disable the daemon.

I did that and ran it with ./gradlew configureTask --no-daemon but got the same result... same error. I'm pretty sure it's not using the daemon as I get the following message: To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/2.13/userguide/gradle_daemon.html.

So if the Gradle daemon is not causing this issue, what else could be? Does anyone more experienced with Gradle know?

Community
  • 1
  • 1
dani
  • 100
  • 7
  • [gradle/gradle/issues/1251](https://github.com/gradle/gradle/issues/1251) is open for this. – mkobit Apr 29 '17 at 15:51

1 Answers1

4

Gradle says it needs to run the build in a subprocess because of something in your build settings:

To honour the JVM settings for this build a new JVM will be forked.

And I guess Gradle creates that subprocess in a way that allows it to grab the output (which is the default with the APIs Java offers to spawn subprocesses). As a result, the subprocess does not have access to I/O of your terminal, and System.console() is null within that process: it is not attached to the system console.

It got me curious so I came up with a script that demonstrates the issue (using Groovy for its conciseness, it's the same thing as Java here):

import java.io.Console

println "Console for main JVM: " + System.console()

Process p1 = new ProcessBuilder("groovy", "-e", "print System.console()")
    .redirectErrorStream(true)
    .start()
p1.waitFor()
println "Console for child JVM: " + p1.text

Process p2 = new ProcessBuilder("groovy", "-e", "println 'Console for child JVM with inherited IO: ' + System.console()")
    .redirectErrorStream(true)
    .inheritIO() // <- this changes everything, as now p2 is attached to system console
    .start()
p2.waitFor()
// No need to (actually cannot) get output of p2, as I/O is inherited by p2 it gets printed to terminal directly

Result:

Console for main JVM: java.io.Console@64cd705f
Console for child JVM: null
Console for child JVM with inherited IO: java.io.Console@3c130745

So Gradle is probably building the subprocess like p1 in my example. And I guess it needs to, because it needs to inspect the output (and not let it go directly to system output).

I think your only solutions are:

  • find a way to get Gradle do the build in the main JVM, without forking. Not a Gradle expert so I don't know how but the message seems to imply it's possible.
  • find another way to get user input. Maybe a Swing dialog? (not very elegant but hey, a build that takes user input is not very elegant in the first place, so the way it is collected does not matter much at this point)
Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • This was the hint I needed; we are using extra JVM parameters and when I removed those settings it worked. Obnoxious, not elegant, but I got the task running! Thanks. – dani May 01 '17 at 20:39