Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

722
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda