如果从`gradle run`执行,`System.console()`返回`null`。 [英] `System.console()` returns `null` if executed from `gradle run`

查看:89
本文介绍了如果从`gradle run`执行,`System.console()`返回`null`。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Java程序:

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());
    }
}

这个简单的构建。 gradle

apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'me.fornever.javaterminal.Main'

当我使用它执行它时 gradle --no-daemon run ,我得到以下输出:

When I'm executing it using gradle --no-daemon run, I get the following output:

Console: null

如果我通过 gradle jar从终端执行它; java -cp'.\build\libs\java-terminal.jar'me.fornever.javaterminal.Main ,我得到以下信息:

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

我知道,当父进程使用stdout重定向时, System.console()可能返回 null 。是否有一些Gradle选项来禁用重定向并使控制台完全可用于我的程序?

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?

我正在为Java开发终端库,所以我想运行测试

I am developing a terminal library for Java, so I want to run my tests and executables without Gradle intervention in stdin/stdout/stderr.

请注意, System.console() null 不是唯一的问题,但也是最明显的问题。实际上,我想从 gradle run 执行的程序中访问WinAPI WriteConsoleW 函数,但是我无法使用由于相同的原因,该功能 System.console() null 。因此,如果此选项可用,我真的需要在Gradle中禁用输出重定向。

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.

还请注意,该问题与 Gradle构建null控制台对象,因为该问题询问如何使用 System.console ()在Gradle脚本中,而不是在 gradle run 调用的Java程序中;我认为他们在这方面的工作方式有所不同,因为这两个答案都无法正常工作或不适用于我的情况。

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.

推荐答案

在为了使 java.io.Console 可用,子进程的stdin和stdout必须指向Linux / macOS / Unix终端或Windows控制台。最简单的安排方式是从已经以这种方式设置的父进程继承stdin和stdout。但是,Gradle( GRADLE-3292 )存在一个已知限制,即 JavaExec 任务类型不支持使子进程继承父进程的输入/输出流。

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.

如果Gradle JavaExec 任务类型得到增强,以支持stdin / stdout继承,然后可以使其适用于 gradle --no-daemon run

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.

使其与Gradle守护程序一起使用将更加复杂,并且可能涉及特定于平台的代码来操纵伪终端或调用Windows控制台API。

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.

这篇关于如果从`gradle run`执行,`System.console()`返回`null`。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆