Package java. Lang;
Import java. Io .*;
Import java. util. stringtokenizer;
/**
*
* The object that controls the Java Virtual Machine. It is a singleton.
*
* Comment by liqiang
*
*/
Public class runtime {
// A unique instance is generated during class initialization.
Private Static runtime currentruntime = new Runtime ();
// Obtain the unique runtime instance
Public static runtime getruntime (){
Return currentruntime;
}
// Private constructor
Private Runtime (){}
/**
*
* Terminate the currently running Java Virtual Machine
*
* It terminates the JVM In the termination order, which includes two parts.
* 1 run all registered threads that close the hook
* 2 if the setting status is disabled, the Finalize method is called for all objects that are not cleared.
*
*/
Public void exit (INT status ){
// Security check
Securitymanager SECURITY = system. getsecuritymanager ();
If (Security! = NULL ){
Security. checkexit (Status );
}
// Exit the VM
Shutdown. Exit (Status );
}
/**
*
* Register a close hook
*
*/
Public void addshutdownhook (thread hook ){
Securitymanager Sm = system. getsecuritymanager ();
If (SM! = NULL ){
SM. checkpermission (New runtimepermission ("shutdownhooks "));
}
Shutdown. Add (Hook );
}
/**
*
* Deregister a registration hook
*
*/
Public Boolean removeshudownhook (thread hook ){
Securitymanager Sm = system. getsecuritymanager ();
If (SM! = NULL ){
SM. checkpermission (New runtimepermission ("shutdownhooks "));
}
Return shutdown. Remove (Hook );
}
/**
*
* Force stop a running Virtual Machine
* It does not run to close the hook thread, nor call the Finalize method of uncleared objects.
*
*/
Public void halt (INT status ){
Securitymanager Sm = system. getsecuritymanager ();
If (SM! = NULL ){
SM. checkexit (Status );
}
Shutdown. Halt (Status );
}
/**
*
* The Finalize method of objects not cleared is called when the VM exits.
* If the value is true, the call is made. If the value is false, the call is not made.
*
*/
Public static void runfinalizersonexit (Boolean value ){
Securitymanager SECURITY = system. getsecuritymanager ();
If (Security! = NULL ){
Try {
Security. checkexit (0 );
} Catch (securityexception e ){
Throw new securityexception ("runfinalizersonexit ");
}
}
Shutdown. setrunfinalizersonexit (value );
}
/*
*
* Local method for executing commands
*
*/
Private native process execinternal (string cmdarray [], string envp [], string path)
Throws ioexception;
/**
*
* Call system commands
* For example, runtime.getruntime(cmd.exe C ("Notepad"); starts a notepad
*
*/
Public process exec (string command) throws ioexception {
Return exec (command, null );
}
/**
*
* Call system commands
*
* @ Param cmd refers to the command line command of the system.
* @ Param envp indicates the command parameter, in the form of name = Value Pair
*
*/
Public process exec (string cmd, string envp []) throws ioexception {
Return exec (CMD, envp, null );
}
/**
*
* Call system commands
*
*/
Public process exec (string command, string envp [], file DIR)
Throws ioexception {
Int COUNT = 0;
// Command Array
String cmdarray [];
Stringtokenizer st;
// Throw an exception when the length of the command string is 0
If (command. Length () = 0)
Throw new illegalargumentexception ("Empty command ");
// Split the command string into an array
St = new stringtokenizer (command );
Count = ST. counttokens ();
Cmdarray = new string [count];
// It is unnecessary to regenerate a stringtokenizer.
St = new stringtokenizer (command );
Count = 0;
While (St. hasmoretokens ()){
Cmdarray [count ++] = ST. nexttoken ();
}
Return exec (cmdarray, envp, DIR );
}
/**
*
* Call system commands
*
*/
Public process exec (string cmdarray []) throws ioexception {
Return exec (cmdarray, null );
}
/**
*
* Call system commands
*
*/
Public process exec (string cmdarray [], string envp []) throws ioexception {
Return exec (cmdarray, envp, null );
}
/**
*
* Call system commands
*
* @ Param cmdarray command Array
* @ Param envp parameter Array
* @ Param dir: the working path of the sub-processing. If it is null, the former working path is used.
*
*/
Public process exec (string cmdarray [], string envp [], file DIR)
Throws ioexception {
// Copy the command Array
Cmdarray = (string []) cmdarray. Clone ();
// If the parameter is not blank, copy the parameter Array
Envp = (envp! = NULL? (String []) envp. Clone (): NULL );
// If the parameter array is an empty array, an exception is thrown.
If (cmdarray. Length = 0 ){
Throw new indexoutofboundsexception ();
}
// Check whether there is a NULL command. If yes, an exception is thrown.
For (INT I = 0; I <cmdarray. length; I ++ ){
If (cmdarray [I] = NULL ){
Throw new nullpointerexception ();
}
}
// If the parameter array is not empty, an exception is thrown if any null item exists in the parameter array.
If (envp! = NULL ){
For (INT I = 0; I <envp. length; I ++ ){
If (envp [I] = NULL ){
Throw new nullpointerexception ();
}
}
}
// Security check
Securitymanager SECURITY = system. getsecuritymanager ();
If (Security! = NULL ){
Security. checkexec (cmdarray [0]);
}
String Path = (DIR = NULL? Null: Dir. getpath ());
// Execute the command
Return execinternal (cmdarray, envp, PATH );
}
/**
*
* Returns the number of JVM processors. The minimum value is 1.
*
*/
Public native int availableprocessors ();
/**
*
* Returns the number of available memories.
*
*/
Public native long freememory ();
/**
*
* Returns the number of memory occupied by the JVM.
*
* Returns the total amount of memory in the Java Virtual Machine.
* The value returned by this method may be vary over time, depending on
* The host environment.
* <P>
* Note that the amount of memory required to hold an object of any
* Given type may be implementation-dependent.
*
* @ Return the total amount of memory currently available for current
* And future objects, measured in bytes.
*/
Public native long totalmemory ();
/**
*
* Returns the maximum memory available for the JVM. The upper limit is related to the actual physical memory.
* The maximum memory size that a process in the current operating system can use is related
*
*/
Public native long maxmemory ();
/**
*
* Garbage collection
*
*/
Public native void GC ();
/* Wormhole for calling java. Lang. Ref. finalizer. runfinalization */
Private Static native void runfinalization0 ();
/**
*
* Run the clearing process.
*
*/
Public void runfinalization (){
Runfinalization0 ();
}
/**
*
* Set whether to display the structure track.
*
*/
Public native void traceinstructions (Boolean on );
/**
*
* Set the method call track
*
*/
Public native void tracemethodcils (Boolean on );
/**
*
* Load the dynamic link library of the specified file name
* Example: runtime. getruntime (). Load ("/home/AVH/lib/libx11.so ");
*
*/
Public void load (string filename ){
Load0 (system. getcallerclass (), filename );
}
// Load the dynamic link library
Synchronized void load0 (class fromclass, string filename ){
Securitymanager SECURITY = system. getsecuritymanager ();
If (Security! = NULL ){
Security. checklink (filename );
}
// If it is not an absolute path, an exception is thrown.
If (! (New file (filename). isabsolute ())){
Throw new unsatisfiedlinkerror (
"Expecting an absolute path of the Library:" + filename );
}
Classloader. loadlibrary (fromclass, filename, true );
}
/**
*
* Load the dynamic link library
*
*/
Public void loadlibrary (string libname ){
Loadlibrary0 (system. getcallerclass (), libname );
}
Synchronized void loadlibrary0 (class fromclass, string libname ){
// Security check
Securitymanager SECURITY = system. getsecuritymanager ();
If (Security! = NULL ){
Security. checklink (libname );
}
// If the file. separatorchar character exists, an exception is thrown.
If (libname. indexof (INT) file. separatorchar )! =-1 ){
Throw new unsatisfiedlinkerror (
"Directory separator shocould not appear in Library name:" + libname );
}
Classloader. loadlibrary (fromclass, libname, false );
}
Public inputstream getlocalizedinputstream (inputstream in ){
Return in;
}
Public outputstream getlocalizedoutputstream (outputstream out ){
Return out;
}
}