What is System.out exactly?

后端 未结 3 1946
既然无缘
既然无缘 2021-01-12 22:28

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 dou

3条回答
  •  余生分开走
    2021-01-12 23:01

    Processes in modern operating systems (and for that matter, several older operating systems) get three standard "streams" associated with them:

    • 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, and System.err are, by default, Java's standard mechanism for writing to those streams.

    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.

    Programs launched via GUIs frequently don't have those streams hooked to anything you can see.

    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.)

    How do I open the console from a Runnable JAR file started by user with double-click?

    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.

    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).

提交回复
热议问题