在 Java 中创建控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2723101/
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-10-29 22:31:30  来源:igfitidea点击:

Creating a console in Java

javaconsole

提问by Puppy

When I try to use java.lang.System.console(), I get a null pointer. I can still write to out and read from in, but this only works when I run straight from my IDE. When I run the .jar file directly, nothing happens. How can I create a console like I'd see using std::cout for use in Java?

当我尝试使用 java.lang.System.console() 时,我得到一个空指针。我仍然可以写出和读入,但这只有当我直接从我的 IDE 运行时才有效。当我直接运行 .jar 文件时,没有任何反应。我如何创建一个控制台,就像我看到的那样,使用 std::cout 在 Java 中使用?

Edit: I was hoping to just create one, rather than understand why I don't have one, since I need one for my program's operation.

编辑:我希望只创建一个,而不是理解为什么我没有一个,因为我的程序运行需要一个。

采纳答案by OscarRyz

Perhaps you're trying to get the console by double-clicking in the jar

也许您正试图通过双击 jar 来获取控制台

Try creating a batch file that opens the console for you.

尝试创建一个为您打开控制台的批处理文件。

You can however create a console using Swing and redirect standard input/output there.

但是,您可以使用 Swing 创建控制台并在那里重定向标准输入/输出。

Source: Create Java console inside a GUI panel

来源:在 GUI 面板中创建 Java 控制台

回答by Pops

How are you running the JAR file exactly? That would be the expected behavior for double-clicking its icon in Windows Explorer, as Kelly alluded to, but not for firing it up from the command line.

你是如何运行 JAR 文件的?正如凯利所暗示的那样,这将是在 Windows 资源管理器中双击其图标的预期行为,但不是从命令行启动它。

From the Console entryin the API (emphasis mine):

从API 中的控制台条目(强调我的):

Whether a virtual machine has a console is dependent uponthe underlying platform and also upon the manner in which the virtual machine is invoked. 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 Kelly S. French

java.lang.System.outand java.lang.System.inare the input/output streams for console access. Java won't create a "console" but allows you to interact with the IO streams provided by the operating system.

java.lang.System.outjava.lang.System.in是控制台访问的输入/输出流。Java 不会创建“控制台”,但允许您与操作系统提供的 IO 流进行交互。

When you run it from a jar file (like clicking on it from a folder) you'll get the I/O streams of the GUI which don't display anywhere.

当您从 jar 文件运行它时(例如从文件夹中单击它),您将获得不会在任何地方显示的 GUI 的 I/O 流。

Try creating a batch file with a 'java -jar ' command in it. When you run the batch file, you should see the command window. I'm assuming windows here. Another way is to run cmd.exe directly with arguments that keep the window open, i.e. "cmd.exe /c".

尝试创建一个包含“java -jar”命令的批处理文件。运行批处理文件时,您应该会看到命令窗口。我假设这里有窗户。另一种方法是使用保持窗口打开的参数直接运行 cmd.exe,即“cmd.exe /c”。

回答by justkt

Instead of running the jar file directly, open a console (you didn't specify an operating system, but this would be the command line on Windows and a console on *Nix, or Terminal on OS X). Then, run java -jar /path/to/your.jar.

不是直接运行 jar 文件,而是打开一个控制台(您没有指定操作系统,但这将是 Windows 上的命令行和 *Nix 上的控制台,或 OS X 上的终端)。然后,运行java -jar /path/to/your.jar

The equivalent of std::coutin Java would be System.outas you probably already know.

您可能已经知道std::cout,Java 中的 等价物System.out

EDIT: With regards to your edit, there is code out there to do this. For example, you can use Swing. There's even a StackOverflow answerwith more than one working code sample.

编辑:关于您的编辑,有代码可以做到这一点。例如,您可以使用Swing。甚至还有一个 StackOverflow 答案,其中包含多个工作代码示例。

回答by Istao

See JConsole, which is a general console in java, used for instance by groovy. Or see groovy directly.

请参阅JConsole,它是 Java 中的通用控制台,例如由 groovy 使用。或者直接看groovy。