到底什么是System.out? [英] What is System.out exactly?
问题描述
我注意到,未从命令行启动的JAR文件(即,用户通过双击启动的可运行JAR文件)对System.out.println()
的任何调用都不会打开控制台.
I noticed that any call to System.out.println()
from a JAR file that hasn't been started by the command line (i.e. a Runnable JAR file started by user with double-click) won't open the console.
进行了一些研究之后,我在网站上找到了多个答案:
After doing some research, I found multiple answers on the site:
这样做是没有问题的. 但是您希望在哪里看到输出?
可执行jar中的"System.out.println()"会发生什么?
如果您以某种不附加控制台的方式运行代码-例如Windows上的
javaw
,这是与可执行jar文件关联的默认程序-那么输出将不会随处可见.它不会引起任何错误-文本只会丢失.
If you run the code in some way that doesn't attach a console - such as
javaw
on Windows, which is the default program associated with executable jar files - then the output won't go anywhere. It won't cause any errors - the text will just be lost.
据我了解,System.out
不代表控制台.它确实表示可以由任何需要显示的数据处理的数据.
我说的对吗?
From what I understand, System.out
does not represent the console. It does represent data which can be handled by anything that needs to display it.
Am I right?
-
System.out
到底是什么? - 如何从用户双击启动的Runnable JAR文件中打开控制台?
- What is
System.out
exactly? - How do I open the console from a Runnable JAR file started by user with double-click?
推荐答案
现代操作系统中的进程(为此,一些较旧的操作系统)获得与之关联的三个标准流":
Processes in modern operating systems (and for that matter, several older operating systems) get three standard "streams" associated with them:
- 标准格式:基于流的输入(
stdin
) - 标准输出:基于流的输出(
stdout
) - 标准错误:基于流的错误输出(
stderr
)
- Standard in: Stream-based input (
stdin
) - Standard out: Stream-based output (
stdout
) - Standard error: Stream-based error output (
stderr
)
集体(创造性地)将它们称为标准流.
Collectively (and creatively) they're called the standard streams.
System.in
,System.out
和System.err
是Java写入这些流的标准机制.
System.in
, System.out
, and System.err
are, by default, Java's standard mechanism for writing to those streams.
从命令行调用的程序在命令行中的击键转到stdin
的环境中运行,并且stdout
和stderr
的输出均在控制台中显示为文本.它们可以重定向到文件等.
Programs invoked from the command line are run in an environment where keystrokes in the command line go to stdin
, and the output of both stdout
and stderr
shows as text in the console. They can be redirected to files, etc.
通过GUI启动的程序经常没有将这些流连接到您可以看到的任何内容.
Programs launched via GUIs frequently don't have those streams hooked to anything you can see.
我在上面说默认",是因为您可以使用System
上的调用来更改这些流指向的位置(它们具有诸如 setOut
和
I say "by default" above because you can use calls on System
to change where those streams point (they have creative names like setIn
, setOut
, and setErr
.)
如何从用户双击启动的Runnable JAR文件中打开控制台?
How do I open the console from a Runnable JAR file started by user with double-click?
那里存在一个错误的关联:jar是可运行的,这并不是为什么您看不到流的事实.如果在命令行上运行可运行的jar,则会看到其标准输出.
There's a false correlation there: The fact that the jar is runnable is not why you don't see the streams. If you run a runnable jar at the command line, you'll see its standard output.
是否在不将其与某种类型的控制台关联的情况下运行该输出是否可以看到,将取决于您如何运行它以及可能如何编写它.许多GUI框架都会将标准重定向到err并返回到日志文件.或者该应用程序可能会提供调试选项.那里没有一个标准答案(没有双关语).
Whether you can see that output if you run it without it being associated with a console of some kind will depend on how you're running it and, potentially, how it's written. Many GUI frameworks will redirect standard out and err to a log file. Or the app may offer debugging options that do so. There's no one standard answer there (no pun).
这篇关于到底什么是System.out?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!