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.