tags:

views:

275

answers:

4

Using System.out (and related) always seemed awkward because of the public field and now with the latest Netbeans gives a blatant "Statement should be removed" hint.

Hence my question: Is System.out still the preferred way to write to the console or is there any other API which should be preferred?

EDIT: I don't want this for logging, but for a simple console application. Something like ps which prints out the running processes to the console.

+4  A: 

Yes, mostly for one reason: There is no other way to write to the console. Even if you use a logging framework, it will eventually use System.out.

Aaron Digulla
A: 

I wouldn't suggest writing directly to console. By using log4j (or similiar) framework you can then configure where you want the output to go without having to change your code.

objects
He wanted to write to the console. No one mentioned logging.
Willi
I didn't mention logging either :) By using a logging framework to write to the console you give yourself added flexibility. Writing to System.out directly is just inflexible, even for a simple console application. For example you may have a command switch to change where the output goes, or the option to change the level of output.
objects
Ok, but using a logging framework (like log4j, what you DID mention) for this purpose feels like hacking
Willi
A: 

Just use a logging framework and instead of using System.out log in debug level and in your development environment (NOT production) set your output to the console and loglevel to debug

Lombo
question.contains("log") ? +1 : -1
Willi
i guess your comment is the other way around given you downvote :P
Lombo
+4  A: 

In a command that is designed to be run from the command line, it is (IMO) reasonable to use System.out and System.err for output and for error messages that the person running the command could reasonably expect to understand. If you are running your application from a decent shell or script, the System.out and/or System.err streams may be redirected to files or another process in a pipeline. AFAIK, using System.out and System.err is the only way to "respect" the user's wishes as far as redirection is concerned. Hence, for most use-cases this is still the best way for a command line application to write output for the user to see.

In JDK 1.6 and later, there also a class called java.io.Console which will give you access to the console. It is not spelled out in the Javadoc, but I suspect that java.io.Console opens a fresh stream to the console, and therefore cannot be redirected. (The moral equivalent of opening "/dev/tty" on an old-school UNIX box.) IMO, you should use this if your application wants to REALLY talk to the user, without the possibility of redirection; e.g. to ask for and then read a password.

However, if your application is GUI command, and particularly if it is intended to run as an unattended service, you should avoid "writing to the console" because:

  • the console streams (System.{out,err} or java.io.Console) may be connected to nothing, or
  • they may end up annoying the user, especially if your application provide no way to suppress the console output.
Stephen C
One problem with trying to use java.io.Console is it won't work if you run your app from inside Eclipse (I suspect it also fails inside any IDE) because of some conditional checking inside the Console class.
hbunny