• Post Reply Bookmark Topic Watch Topic
  • New Topic

System class and available methods  RSS feed

 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
Though I have been programming for some time now
I am kind of ashamed to say this but have still not really got the hang of :

1 >
The class "System" exposes a no of methods

What is the difference between : System.out.println & System.err
Both seem to give me a handle to the "printStream" object

I always seem to be using to use : System.out.println
( for debugging . accepted I should use log4j or log mechanism available in 1.4 but thats not the point here )

Question is - where practically would we use System.err.println() ?
In web applications is System.err.println() ever used and if so where ?

2 >
System.exit >> is there any difference beyween System.exit(0) and System.exit(1) ?

Tried reading the net - but havent come up with satisfactory explanations .

I would be happy to learn if someone can give me some pointers.

Regards ,
-anagha
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between System.out.print and System.err.print is that they can be piped to different files. This is especially important for web servers since System.out.print messages are common as log files and System.err.print are generally uncommon and should be logged and addressed quickly.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you start a program, for the operating system it is a simple function call, let say name it main. It tells to start a virtual machine and then the VM call a special method (main method) on the �main� class. When the class ends it returns the power to the VM which in turn returns to the operating system. When the VM returns to the system it returns a int. This is an old standard. In the past a 0 meant a smooth run, anything different meant a problem (normally a error code). Lets say you have a program that reads something from a file, but if the file doesn�t exists it means a 1, then it tries to write to a file if it can�t write to the file it means a 2. So you run the program and get a 1, so you are sure the error cause was it wasn�t able to read from the file.

All these practices are used in batch process, so when the last process reports a 0 you know you can call the next step, but if something reports different from 0 you have to stop all the batch, cause still running will probably result in messing all your system.
 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott and Jamie for your replies .

One question though - Why would "System.err" be important for webservers ?

My understanding of webservers was to purely treat static html content

All dynamic content would be treated by app server / servlet engine

Am I missing something ?

Regards ,
-anagha
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing that makes either System.out or System.err "special" when you're talking about webservers. A webserver is just a program, just like any other program.

Normally, System.out and System.err both print to the same console window. Why do we have these two in Java? It's one of the things that Java inherited from the C and C++ programming languages and Unix.

Here is a good explanation: Standard streams (Wikipedia)
 
Pavithra Praveen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to know how System.out.println() and System.err output can be piped to two different files.......please reply its urgent....
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how it works in Windows: Using command redirection operators

Normally, if you redirect the output of a program to a file using ">" on the command line, only the output of System.out is redirected to the file. What you send to System.err still appears in the console window, and not in the file.

You can redirect System.err to System.out in Windows by adding "2>&1" to the command line. If you do this, then the output of System.err will also be sent to the file instead of the console window.

java com.mypackage.MyClass > example.log 2>&1

As far as I know it is not possible in Windows to redirect out and err to two different files - you can only redirect a stream to another stream, not to a specific file.

Note, it works differently on other operating systems.
[ August 22, 2007: Message edited by: Jesper Young ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
As far as I know it is not possible in Windows to redirect out and err to two different files - you can only redirect a stream to another stream, not to a specific file.
[ August 22, 2007: Message edited by: Jesper Young ]


unknown > stdout.out 2>stderr.out

works on Windows XP. In this case, stderr.out would contain

'unknown' is not recognized as an internal or external command,
operable program or batch file.
 
Pavithra Praveen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually my problem is, i am working on webserver which some what like tomacat. i have developed a web application which is going to run on that webserver. In that servlet class i have written SOP and System.err statements to debug. For that server, all SOP output should go to servlet.log file and all System.err output should be piped to servlet_err.log file. But while running my web application in that server, all SOP and System.err output is going to servlet.log file. which is not expected. So for i want to know is that problem with java or i have to configure webserver.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!