I have this simple Java program:
package me.fornever.javaterminal;
public class Main {
public static void main(String[] args) {
System.out.println("Console: " + System.console());
}
}
And this simple build.gradle
:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'me.fornever.javaterminal.Main'
When I'm executing it using gradle --no-daemon run
, I get the following output:
Console: null
If I execute it from the terminal through gradle jar; java -cp '.\build\libs\java-terminal.jar' me.fornever.javaterminal.Main
, I get the following:
Console: java.io.Console@3d4eac69
I am aware that System.console()
may return null
when the parent process uses stdout redirection. Is there some Gradle option to disable the redirection and make the console fully available for my program?
I am developing a terminal library for Java, so I want to run my tests and executables without Gradle intervention in stdin/stdout/stderr.
Please note that System.console()
being null
is not the only issue but the most obvious one. In reality I want to access WinAPI WriteConsoleW
function from the program executed by gradle run
, and I'm unable to use this function due to the same reasons System.console()
being null
. So I really need to disable output redirection in Gradle if this option is available.
Also please note that the question is different from Gradle build null console object because that question asks how to use System.console()
inside of a Gradle script and not in the Java program invoked by gradle run
; I believe they're working differently in that matter, because neither of the answers are working or applicable to my case.
System.console() returns null if there is no console. You can work round this either by adding a layer of indirection to your code or by running the code in an external console and attaching a remote debugger. It also returned null for me in PowerShell when running something like java -cp ... Class | tee out.
2 1 This answer lead me to find that if you are running Gradle with the deamon enabled, you will not be able to get a console in your Gradle tasks. Running my Gradle task with the ' --no-daemon' allows console access.
Note: This property is deprecated and will be removed in the next major version of Gradle. Note: This property has been replaced by executionResult. The result for the command run by this task.
Add a comment | 16 System.console()returns null if there is no console. You can work round this either by adding a layer of indirection to your codeor by running the code in an external console and attaching a remote debugger. Share Follow edited May 23 '17 at 11:46
In order for java.io.Console
to be available, the child processes' stdin and stdout have to point to a Linux/macOS/Unix terminal or a Windows console. The easiest way to arrange that is to inherit the stdin and stdout from a parent process already set up that way. However, there is a known limitation of Gradle (GRADLE-3292) that the JavaExec
task type does not support making the child process inherit the input/output streams of the parent process.
If the Gradle JavaExec
task type was enhanced to support stdin/stdout inheritance, then this could be made to work for gradle --no-daemon run
.
Getting it to work with the Gradle daemon would be far more complex, and likely involve platform-specific code to manipulate pseudoterminals or call the Windows console API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With