|Start of Tutorial > Start of Trail||
Sometimes, a program requires access to system resources such as system properties, standard input and output, or the current time. Your program could make system calls directly to the window or operating system, but then your program would be able to run only in that particular environment. Each time you want to run the program in a new environment, you'd have to port your program by rewriting the system-dependent sections of code.
The Java platform lets your program access system resources through a (relatively) system-independent API implemented by the
Systemclass and through a system-dependent API implemented by the
Purity Tip: Some of the system resources available through the
Runtimeclasses cannot be used in 100% Pure Java programs. These resources are noted throughout this lesson.
Most system programming needs are met through the
Systemclass. However, in rare cases, a program might have to access the system through the
Runtimeobject that represents the current runtime environment. The last section of this lesson, The
RuntimeObject explains how to do this and talks about the trade-offs of accessing the system directly via the Runtime object.
The following diagram shows that the
Systemclass allows your Java programs to use system resources but insulates them from system-specific details.
If you've experimented with other lessons in this tutorial, you've no doubt already seen the
Systemclass's standard output stream used in several examples to display text. This and other resources available through
Systemare briefly described here and covered in the sections indicated.
System's methods and variables are class methods and class variables. You don't instantiate the
Systemclass to use it; you use the
Systemclass's methods and variables directly from a reference to the
Probably the most frequently used items from the
Systemclass are the streams used for reading and writing text. The
Systemclass provides one stream for reading text--the standard input stream--and two streams for writing text--the standard output and standard error streams.
Properties are key/value pairs that your Java programs can use to set up various attributes or parameters between invocations. The Java platform itself maintains a set of system properties that contain information about the current platform. You can access the system properties through the
In Java, you don't have to free an object when you're done with it-the garbage collector runs periodically in the background and cleans up unreferenced objects. Or you can force the garbage collector to run using
gcmethod. Also, you can request that the runtime system perform object finalization using
The security manager is an application-wide object that determines whether potentially threatening operations should be allowed. You use the
Systemclass to set and get the security manager for an application. Subclasses of
java.lang.SecurityManagerimplement a specific management policy.
Systemclass includes several miscellaneous methods that let you get the current time in milliseconds, exit the interpreter, and copy arrays.
Most system programming needs are met through the programming interface provided by the
Systemclass. However, in rare cases, a program must bypass the system-independent interface of the
Systemclass and use system resources directly from the runtime environment. The Java environment provides a
Runtimeobject, which represents the current runtime environment. You can use a
Runtimeobject to access system resources directly.
Note: Messaging the
Runtimeobject directly compromises your ability to run your program on different systems. You should do this only in special situations.
|Start of Tutorial > Start of Trail||
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.