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

Monday, January 19, 2009

Runtime Class

You can't construct a Runtime instance yourself. A Runtime object is obtained by calling the static method Runtime.getRuntime(). By using a Runtime instance, you can

  • Execute a subprocess.
  • Exit the program.
  • Load a dynamically linked library.
  • Run the garbage collector or finalize objects.
  • Estimate free memory.
  • Control program tracing.
  • Localize streams (make them translate from Unicode to the local character set).


A Runtime object can be used to execute another system process by way of the exec() method (in four flavors) that returns a java.lang.Process object.


The Process instance is useful for attaching to the standard input, output, and error streams of the new process.


You can also kill the subprocess, wait for it to terminate, and retrieve its exit code, all by way of the Process object.


The process exists outside the Java virtual machine. It is not a Thread but a separate system process, and some aspects of its behavior may be system-dependent.


You can use a Runtime object to load a dynamically linked library. This is necessary in order to use native methods in Java.


The methods traceInstructions() and traceMethodCalls() request that the Java virtual machine print trace information about each instruction or each method call that gets executed, respectively. Where the output ends up or whether tracing is supported at all is implementation-dependent.


While you can use a Runtime object to run the garbage collector or finalize any outstanding objects (gc() and runFinalization() methods), this should not normally be necessary, since the Java environment runs a separate thread whose purpose is to finalize and garbage collect when necessary.


Furthermore, although the exit() method can be used to exit the program, it should normally be avoided, except to specify an exit code upon normal termination of a stand-alone program. Low-level methods and applets generally throw exceptions instead.

No comments:

Post a Comment

Followers