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?
System.out
exactly?Java System. out. println() is used to print an argument that is passed to it.
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.
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.
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.
Processes in modern operating systems (and for that matter, several older operating systems) get three standard "streams" associated with them:
stdin
)stdout
)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).
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With