Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

721
Views
Gradle console() is null with daemon disabled

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!