Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is System.out exactly?

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:

  • System.out.println in jar

    There is no problem doing like that. But where do you expect to see the output?

  • What happens to “System.out.println()” in executable 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.

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?

  • What is System.out exactly?
  • How do I open the console from a Runnable JAR file started by user with double-click?
like image 789
Atom 12 Avatar asked Aug 29 '15 15:08

Atom 12


People also ask

What is the use of System out in Java?

Java System. out. println() is used to print an argument that is passed to it.

Why do we use System out?

System. out refers to a PrintStream to which you can print characters. It is usually used to display results or outputs of execution in the screen console or terminal.

What is the output of System out?

System. out is a static object of type PrintStream . It holds a reference to the standard output stream which is usually the console. Usually, printing to the console is useful when you want to interact or display messages to the user in a console application, log status and error messages to the console.

What is System out Println () explain in detail?

In java, we use System. out. println() statement to display a message, string or data on the screen. It displays the argument that we pass to it.


3 Answers

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

like image 128
T.J. Crowder Avatar answered Oct 18 '22 10:10

T.J. Crowder


Here, System.out represents the output stream - where your output will go. By default it is set to console. But you can change it to other like - text file. Most often, in large application it is used for logging (usually by new programmer, bad idea). In this case you can see the output in appropriate log file.

like image 37
Razib Avatar answered Oct 18 '22 11:10

Razib


System is final class from java.lang package(default package in java) and cannot be instantiated.

out is a static member field of System class and is of type PrintStream and its access specifiers are public final.

println – is an overloaded method of PrintStream class. println prints the argument passed to the standard console and a newline. There are multiple println overloaded methods with different arguments. Every println makes a call to print method and adds a newline. Internally, print calls write() and write() takes care of displaying data to the standard output window.

Here it is how it should look in the inside:

//the System class belongs to java.lang package
class System {
  public static final PrintStream out;
  //...
}

//the Prinstream class belongs to java.io package
class PrintStream{
public void println();
//...
}

We therefore don't need to ever instantiate a System object to print messages to the screen; we simply call the println method on the System class's public static PrintStream member, out.

But you cannot create an object of PrintStream and call the println function. When you want to print to the standard output, then you will use System.out. That's the only way. Instantiating a PrintStream will allow you to write to a File or OutputStream you specify, but don't have anything to do with the console.

However, you can pass System.out to PrintStream and then invoke println on PrintStream object to print to the standard output. Here is a small example:

import java.io.*;
public class SystemOutPrintlnDemo
{
  public static void main(String[] args) 
  {
    //creating PrintStream object
    PrintStream ps = new PrintStream(System.out);
    ps.println("Hello World!");
    ps.print("Hello World Again!");
    //Flushes the stream
    ps.flush();
  }
}
like image 36
burglarhobbit Avatar answered Oct 18 '22 09:10

burglarhobbit