|
|
Table of Contents |
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
Runtimeclass.
Purity Tip: Some of the system resources available through theSystemandRuntimeclasses 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 theRuntimeobject that represents the current runtime environment. The last section of this lesson, TheRuntimeObject 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.[PENDING: get figure from book]
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 throughSystemare briefly described here and covered in the sections indicated.Using the
SystemClassAll ofSystem's methods and variables are class methods and class variables. You don't instantiate theSystemclass to use it; you use theSystemclass's methods and variables directly from a reference to theSystemclass.The Standard I/O Streams
Probably the most frequently used items from theSystemclass are the streams used for reading and writing text. TheSystemclass provides one stream for reading text--the standard input stream--and two streams for writing text--the standard output and standard error streams.
SystemPropertiesProperties 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 theSystemclass.Forcing Finalization and Garbage Collection
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 usingSystem'sgcmethod. Also, you can request that the runtime system perform object finalization usingSystem'srunFinalizationmethod.Providing Your Own Security Manager
The security manager is an application-wide object that determines whether potentially threatening operations should be allowed. You use theSystemclass to set and get the security manager for an application. Subclasses ofjava.lang.SecurityManagerimplement a specific management policy.Miscellaneous
SystemMethodsTheSystemclass includes several miscellaneous methods that let you get the current time in milliseconds, exit the interpreter, and copy arrays.The
RuntimeObjectMost system programming needs are met through the programming interface provided by theSystemclass. However, in rare cases, a program must bypass the system-independent interface of theSystemclass and use system resources directly from the runtime environment. The Java environment provides aRuntimeobject, which represents the current runtime environment. You can use a
Runtimeobject to access system resources directly.
Note: Messaging theRuntimeobject directly compromises your ability to run your program on different systems. You should do this only in special situations.
|
|
Table of Contents |