Java System.console() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4203646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:31:13  来源:igfitidea点击:

System.console() returns null

javaeclipseconsolespringsource

提问by Gauls

I was using readLineof BufferedReaderto get input/new password from user, but wanted to mask the password so I am trying to use java.io.Consoleclass. Problem is that System.console()returns nullwhen an application is debugged in Eclipse. I am new to Java and Eclipse not sure is this the best way to achieve? I am right clicking on the source file and selecting "Debug As" > "Java Application". Is there any workaround?

我正在使用readLineofBufferedReader从用户那里获取输入/新密码,但想屏蔽密码,所以我尝试使用java.io.Console类。问题是在 Eclipse 中调试应用程序时System.console()返回null。我是 Java 和 Eclipse 的新手,不确定这是实现的最佳方式吗?我右键单击源文件并选择“调试为”>“Java 应用程序”。有什么解决方法吗?

回答by Stephen C

That is right.

没错。

You will have to run the application outside of Eclipse. Look at the launcher configuration panels within Eclipse and see if you can spot the option that says to run the command in a separate JVM.

您必须在 Eclipse 之外运行该应用程序。查看 Eclipse 中的启动器配置面板,看看您是否可以找到在单独的 JVM 中运行命令的选项。

回答by Andrzej Doyle

I believe that in the run configurations for Eclipse, you can configure whether to assign a console or not - ensure this is checked. (It's been a while since I used Eclipse so I can't give specific instructions I'm afraid).

我相信在 Eclipse 的运行配置中,您可以配置是否分配控制台 - 确保选中此项。(自从我使用 Eclipse 已经有一段时间了,所以恐怕我不能给出具体的说明)。

If that doesn't work, then something that will definitely do this job is starting your application in debug mode, then connect to the process with Eclipse. Search for "eclipse remote debugging" if you're not sure how to do this.

如果这不起作用,那么肯定可以完成这项工作的是在调试模式下启动您的应用程序,然后使用 Eclipse 连接到该进程。如果您不确定如何执行此操作,请搜索“eclipse 远程调试”。

Furthermore, in general it is a bad idea to requirea console to be assigned as this very much impacts the flexibility of your application - as you've just discovered. Many ways of invoking Java will not assign a console, and your application is unusable in these instances (which is bad). Perhaps you could alternatively allow arguments to be specified on the command line. (If you're testing the console input specifically then fair enough, but it would potentially be useful for people to be able to invoke your application from scripts and/or on headless servers, so this sort of flexible design is almost always a good idea. It often leads to better-organised code, too.)

此外,通常要求分配控制台是一个坏主意,因为这会极大地影响您的应用程序的灵活性 - 正如您刚刚发现的那样。许多调用 Java 的方法不会分配控制台,并且您的应用程序在这些实例中无法使用(这很糟糕)。也许您也可以允许在命令行上指定参数。(如果您正在专门测试控制台输入,那么就足够公平了,但是人们能够从脚本和/或无头服务器上调用您的应用程序可能很有用,因此这种灵活的设计几乎总是一个好主意.它也经常导致更好的组织代码。)

回答by Vincent Ramdhanie

According to the docs:

根据文档

If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

如果虚拟机是自动启动的,例如由后台作业调度程序启动,那么它通常没有控制台。

回答by McDowell

System.console()returns null if there is no console.

System.console()如果没有控制台,则返回 null。

You can work round this either by adding a layer of indirection to your codeor by running the code in an external console and attaching a remote debugger.

您可以通过向代码添加一个间接层或通过在外部控制台中运行代码并附加远程调试器来解决此问题

回答by Kane

add -consolein your program arguments to start OSGi console

在程序参数中添加-console以启动 OSGi 控制台

回答by swimmingfisher

This is a bug #122429of eclipse

这是eclipse 的一个bug #122429

回答by formixian

This code snippet should do the trick:

这个代码片段应该可以解决问题:

private String readLine(String format, Object... args) throws IOException {
    if (System.console() != null) {
        return System.console().readLine(format, args);
    }
    System.out.print(String.format(format, args));
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            System.in));
    return reader.readLine();
}

private char[] readPassword(String format, Object... args)
        throws IOException {
    if (System.console() != null)
        return System.console().readPassword(format, args);
    return this.readLine(format, args).toCharArray();
}

While testing in Eclipse, your password input will be shown in clear. At least, you will be able to test. Just don't type in your real password while testing. Keep that for production use ;).

在 Eclipse 中进行测试时,您输入的密码将清晰显示。至少,您将能够进行测试。只是不要在测试时输入您的真实密码。保留用于生产用途;)。

回答by MonkeyPaperwork

I also ran into this problem when trying to write a simple command line application.

我在尝试编写一个简单的命令行应用程序时也遇到了这个问题。

Another alternative to creating your own BufferedReader object from System.in is to use java.util.Scanner like this:

从 System.in 创建自己的 BufferedReader 对象的另一种替代方法是使用 java.util.Scanner ,如下所示:

import java.util.Scanner;

Scanner in;
in = new Scanner(System.in);

String s = in.nextLine();

Of course this will not be a drop-in replacement to Console, but will give you access to a variety of different input functions.

当然,这不是 Console 的直接替代品,但可以让您访问各种不同的输入功能。

Here's more documentation on Scanner from Oracle.

这是有关 Oracle 扫描仪更多文档

回答by Emad Aghayi

According to the API:

根据API

"If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console."

“如果虚拟机是从交互式命令行启动而不重定向标准输入和输出流,那么它的控制台将存在,并且通常会连接到启动虚拟机的键盘和显示器。如果虚拟机已启动自动,例如通过后台作业调度程序,那么它通常不会有控制台。”

回答by Ojonugwa Jude Ochalifu

Got this error message when running the application from Netbeans. Judging from the other answers, it seems this happens when running your application from an IDE. If you take a look at this question: Trying to read from the console in Java, it is because

从 Netbeans 运行应用程序时收到此错误消息。从其他答案来看,从 IDE 运行应用程序时似乎会发生这种情况。如果你看一下这个问题:Trying to read from the console in Java,那是因为

Most IDEs are using javaw.exe instead of java.exe to run Java code

大多数 IDE 使用 javaw.exe 而不是 java.exe 来运行 Java 代码

The solution is to use the command line/terminalto get the Console.

解决方案是使用command line/terminal来获取Console.