繁体   English   中英

如何检查 Java 程序的输入/输出流是否已连接到终端?

[英]How can I check if a Java program's input/output streams are connected to a terminal?

我希望 Java 程序根据其用途具有不同的默认设置(详细程度,可能支持彩色输出)。 在 C 中,有一个 isatty() 函数,如果文件描述符连接到终端,它将返回 1,否则返回 0。 在 Java 中是否有这样的等价物? 我在 JavaDoc 中没有看到 InputStream 或 PrintStream 的任何内容。

System.console() 与 isatty()

System.console(),正如@Bombe 已经提到的,适用于检查控制台连接的简单用例。 然而,System.console() 的问题在于它不能让您确定连接到控制台的是 STDIN 还是 STDOUT(或两者都不是)。

Java 的System.console()和 C 的isatty()之间的区别可以在以下案例分解中说明(我们将数据传入/传出假设的 Foo.class):

1) STDIN 和 STDOUT 是 tty

%> java Foo
System.console() => <Console instance>
isatty(STDIN_FILENO) => 1
isatty(STDOUT_FILENO) => 1

2) STDOUT 是 tty

%> echo foo | java Foo
System.console() => null
isatty(STDIN_FILENO) => 0
isatty(STDOUT_FILENO) => 1

3) STDIN 是 tty

%> java Foo | cat
System.console() => null
isatty(STDIN_FILENO) => 1
isatty(STDOUT_FILENO) => 0

4) STDIN 和 STDOUT 都不是 tty

%> echo foo | java Foo | cat
System.console() => null
isatty(STDIN_FILENO) => 0
isatty(STDOUT_FILENO) => 0

我无法告诉您为什么 Java 不支持更好的 tty 检查。 我想知道某些 Java 的目标操作系统是否不支持它。

使用 JNI 调用 isatty()

从技术上讲可以使用一些相当简单的 JNI在 Java 中执行此操作(如 stephen-c@ 指出的那样),但它会使您的应用程序依赖于可能无法移植到其他系统的 C 代码。 我能理解有些人可能不想去那里。

JNI 外观的快速示例(掩盖了很多细节):

Java: tty/TtyUtils.java

public class TtyUtils {
    static {
        System.loadLibrary("ttyutils");
    }
    // FileDescriptor 0 for STDIN, 1 for STDOUT
    public native static boolean isTty(int fileDescriptor);
}

C: ttyutils.c (假设匹配ttyutils.h ),编译为libttyutils.so

#include <jni.h>
#include <unistd.h>

JNIEXPORT jboolean JNICALL Java_tty_TtyUtils_isTty
          (JNIEnv *env, jclass cls, jint fileDescriptor) {
    return isatty(fileDescriptor)? JNI_TRUE: JNI_FALSE;
}

其他语言:

如果您可以选择使用另一种语言,我能想到的大多数其他语言都支持 tty-checking。 但是,既然你问了这个问题,你可能已经知道了。 我首先想到的(除了 C/C++)是RubyPythonGolangPerl

System.console()将返回您的应用程序连接到的控制台,否则返回null (请注意,它仅从 JDK 6 开始可用。)

简短的回答是,在标准 Java 中没有直接等价于 'isatty' 的东西。 还有的是一个RFE用于在Java错误数据库这样的事情,因为1997年,但它只是 1一个可怜的票。

理论上,您可以使用 JNI 魔法实现“isatty”。 但这会带来各种潜在的问题。 我什至不会考虑自己做这个......


1 - 在 Oracle 接管 Sun 的时候,对修复 Java 错误的投票停止了。

您可以使用jnr-posix库从 Java 调用本机 posix 方法:

import jnr.posix.POSIX;
import jnr.posix.POSIXFactory;
import java.io.FileDescriptor;

POSIX posix = POSIXFactory.getPOSIX();

posix.isatty(FileDescriptor.out);

如果不想自己编译 C 源代码,可以使用 Jansi 库。 它比 jnr-posix 小很多

<dependency>
  <groupId>org.fusesource.jansi</groupId>
  <artifactId>jansi</artifactId>
  <version>1.17.1</version>
</dependency>

...

import static org.fusesource.jansi.internal.CLibrary.isatty;

...

System.out.println( isatty(STDIN_FILENO) );

还有另一种方法。 我偶然发现了这一点,因为我需要使用/dev/tty 我注意到一个FileSystemException被引发,当 Java 程序试图从 tty 设备文件创建一个InputStream ,如果程序不是 TTY 的一部分,比如 Gradle 守护进程。 但是,如果 stdin、stdout 或 stderr 中的任何一个连接到终端,则此代码不会引发带有消息的异常:

  • 在 macOS 上(Device not configured)
  • 在 Linux 上No such device or address

不幸的是,检查/dev/tty存在并且可读将是真的。 此 FSE 仅在实际尝试从文件中读取时发生,而不是读取它。

// straw man check to identify if this is running in a terminal
// System.console() requires that both stdin and stdout are connected to a terminal
// which is not always the case (eg with pipes).
// However, it happens that trying to read from /dev/tty works
// when the application is connected to a terminal, and fails when not
// with the message
//     on macOS '(Device not configured)'
//     on Linux 'No such device or address'
//
// Unfortunately Files::notExists or Files::isReadable don't fail.
//noinspection EmptyTryBlock
try (var ignored = Files.newInputStream(Path.of("/dev/tty"))) {
  return "in a tty"
} catch (FileSystemException fileSystemException) {
  return "not in a tty";
}

虽然这种方法很丑陋,但它避免了使用第三方库。 这并没有回答哪个标准流连接到终端的问题,因为我最好依赖终端库,如 Jansi 或 JLine 3。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM