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
System
class and through a system-dependent API implemented by theRuntime
class.
Purity Tip: Some of the system resources available through theSystem
andRuntime
classes cannot be used in 100% Pure Java programs. These resources are noted throughout this lesson.
Most system programming needs are met through the
System
class. However, in rare cases, a program might have to access the system through theRuntime
object that represents the current runtime environment. The last section of this lesson, TheRuntime
Object 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
System
class 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
System
class's standard output stream used in several examples to display text. This and other resources available throughSystem
are briefly described here and covered in the sections indicated.Using the
System
ClassAll ofSystem
's methods and variables are class methods and class variables. You don't instantiate theSystem
class to use it; you use theSystem
class's methods and variables directly from a reference to theSystem
class.The Standard I/O Streams
Probably the most frequently used items from theSystem
class are the streams used for reading and writing text. TheSystem
class provides one stream for reading text--the standard input stream--and two streams for writing text--the standard output and standard error streams.
System
PropertiesProperties 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 theSystem
class.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
'sgc
method. Also, you can request that the runtime system perform object finalization usingSystem
'srunFinalization
method.Providing Your Own Security Manager
The security manager is an application-wide object that determines whether potentially threatening operations should be allowed. You use theSystem
class to set and get the security manager for an application. Subclasses ofjava.lang.SecurityManager
implement a specific management policy.Miscellaneous
System
MethodsTheSystem
class includes several miscellaneous methods that let you get the current time in milliseconds, exit the interpreter, and copy arrays.The
Runtime
ObjectMost system programming needs are met through the programming interface provided by theSystem
class. However, in rare cases, a program must bypass the system-independent interface of theSystem
class and use system resources directly from the runtime environment. The Java environment provides aRuntime
object, which represents the current runtime environment. You can use aRuntime
object to access system resources directly.
Note: Messaging theRuntime
object directly compromises your ability to run your program on different systems. You should do this only in special situations.
Table of Contents |