CoreJava Important Tricks | Tips | Java Linux IDE | Technology Updates | Download Free CoreJava Examples | Video Tuorials | Concepts of Classes

Monday, January 19, 2009

System Class

There are no instances of the System class. The System class allows you to

  • Access the standard input, output, and error streams.
  • Exit the program.
  • Load a dynamically linked library.
  • Run the garbage collector or finalize objects.
  • Access system Properties.
  • Access the SecurityManager.
  • Perform system-dependent array copy and time-check operations.


The standard input, output, and error streams of your Java application or applet are accessed as System.in, System.out, and System.err.


These class variables are PrintStream objects (see java.io, below), allowing your application to perform the usual UNIX-style I/O.That's not much use in a finished applet, since an applet embedded in a web page is typically disallowed from doing anything useful with these streams. They are handy for debugging in appletviewer, and also in stand-alone Java applications.


Java also maintains some system properties, accessible through the System class. These take the place of environment variables and anything else in the system that is relevant to the Java environment.


The static method getProperties() returns a java.util.Properties object describing the system properties. For example, the properties can be listed by a little program such as the following:

public class Props
{
public static void main( String args[] )
{
System.getProperties().list(System.err);
}
}

This program results in a list of system properties:

-- listing properties --
java.home=/mnt2/java
java.version=1.0
file.separator=/
line.separator=

java.vendor=Sun Microsystems Inc.
user.name=korpen
os.arch=sparc
os.name=Solaris
java.vendor.url=http://www.sun.com/
user.dir=/nfs/grad/korpen/www/java
java.class.path=.:/home/grad/korpen/www/java:/mnt2/ja...
java.class.version=45.3
os.version=2.x
path.separator=:
user.home=/homes/ritesh

Use the static method getProperty() to get individual properties by name.

No comments:

Post a Comment

Followers