Is there a way to determine if Java System.in is “interactive”?

前端 未结 2 481
醉酒成梦
醉酒成梦 2021-01-06 00:18

If the input is interactive, i.e. from the console, I want to print a command prompt e.g. \">\"

But if it is redirected e.g. from a file, then I do not want to promp

2条回答
  •  一整个雨季
    2021-01-06 01:05

    AFAIK, there is no way to do this in pure Java, and even doing it in JNI / JNA would be complicated.

    An alternative might be to use the new Console API introduced in JDK 1.6. This allows you to try to get a reader/writer for the console. If it succeeds, the result is guaranteed to be interactive ... in the sense that you mean.

    A second alternative would be to check do the legwork in the wrapper script that you use to launch your application, and pass the information to Java via the system properties. For instance, on a GNU/Linux system the tty(1) command can be used to tell if stdin is a connected to a "tty" device.

提交回复
热议问题