为什么System.console()为命令行应用程序返回null? [英] Why does System.console() return null for a command line app?
问题描述
我正在使用一个依赖于用户命令行输入的旧版应用程序:
String key = System.console .readLine(请输入许可证密钥:);但是,我得到一个 NullPointerException
因为 System.console()
给我一个null。
为什么 System.console )
为命令行应用程序返回null?
解决方案如果从终端窗口启动java,
如果你直接从终端运行一个简单的测试,它工作?
echo'public class Test {public static void main(String [] args){System.console printf(hello world%n);}}'> Test.java&&& javac Test.java&& java Test
预期输出:
hello world
如果不起作用,
但是,如果它可以正常工作,而你的程序没有,那么你的程序是如何启动的。
检查java二进制文件是如何开始的?它是从shell脚本启动的吗?检查stdin / stdout没有被重定向或管道输入到一些东西,并且可能还没有在后台启动。
ex:这可能会使System.console )return null。
tee> app.log
和此:
java Test> /tmp/test.log
这似乎工作在我的机器(linux)
java测试&
似乎 System.setOut
, System.setErr
或 System.setIn
会影响控制台,即使经过几个gc和finalizer。
不过,
关闭(原始) System.out
或 System.in
将禁用控制台。
echo'public class Test {public static void main(String [] args){System.out.close .console()。printf(hello world%n);}}'> Test.java&& javac Test.java&& java Test
预期输出:
线程main中的异常java.lang.NullPointerException
at Test.main(Test.java:1)
So;扫描您的代码以查找关闭流的位置,或将System.out传递到可能关闭的位置。
I am working on a legacy app which depends on user command line input:
String key = System.console().readLine("Please enter the license key: ");
However, I am getting a NullPointerException
because System.console()
gives me a null.
Why does System.console()
return null for a command line app? It happens when running it out of the terminal as well as IDE.
解决方案 If you start java from a terminal window, then it really should work, even though I haven't tried on OSX.
If you run a simple test using java directly from the terminal, does it work?
echo 'public class Test { public static void main(String[] args) {System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test
Expected output:
hello world
If it doesn't work, then sorry, no console support on your platform.
However, if it works, and your program doesn't then there is a problem with how your program is started.
Check how the java binary started? Is it started from a shell script? Check that stdin/stdout have not been redirected or piped into something, and possibly also that it's not started in the background.
ex: This will probably make System.console() return null.
java Test | tee >app.log
and this:
java Test >/tmp/test.log
This seems to work on my machine (linux)
java Test &
Neither does it seem as if System.setOut
, System.setErr
or System.setIn
affects the console, even after a couple of gc's and finalizers.
However:
Closing (the original) System.out
or System.in
will disable the console
too.
echo 'public class Test { public static void main(String[] args) {System.out.close();System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test
Expected output:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:1)
So; scan your code for places where it closes streams, or passes System.out somewhere it might get closed.
这篇关于为什么System.console()为命令行应用程序返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!