1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.*;
  29 import java.util.StringTokenizer;
  30 import sun.reflect.CallerSensitive;
  31 import sun.reflect.Reflection;
  32 
  33 /**
  34  * Every Java application has a single instance of class
  35  * <code>Runtime</code> that allows the application to interface with
  36  * the environment in which the application is running. The current
  37  * runtime can be obtained from the <code>getRuntime</code> method.
  38  * <p>
  39  * An application cannot create its own instance of this class.
  40  *
  41  * @author  unascribed
  42  * @see     java.lang.Runtime#getRuntime()
  43  * @since   JDK1.0
  44  */
  45 
  46 public class Runtime {
  47     private static Runtime currentRuntime = new Runtime();
  48 
  49     /**
  50      * Returns the runtime object associated with the current Java application.
  51      * Most of the methods of class <code>Runtime</code> are instance
  52      * methods and must be invoked with respect to the current runtime object.
  53      *
  54      * @return  the <code>Runtime</code> object associated with the current
  55      *          Java application.
  56      */
  57     public static Runtime getRuntime() {
  58         return currentRuntime;
  59     }
  60 
  61     /** Don't let anyone else instantiate this class */
  62     private Runtime() {}
  63 
  64     /**
  65      * Terminates the currently running Java virtual machine by initiating its
  66      * shutdown sequence.  This method never returns normally.  The argument
  67      * serves as a status code; by convention, a nonzero status code indicates
  68      * abnormal termination.
  69      *
  70      * <p> The virtual machine's shutdown sequence consists of two phases.  In
  71      * the first phase all registered {@link #addShutdownHook shutdown hooks},
  72      * if any, are started in some unspecified order and allowed to run
  73      * concurrently until they finish.  In the second phase all uninvoked
  74      * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
  75      * has been enabled.  Once this is done the virtual machine {@link #halt
  76      * halts}.
  77      *
  78      * <p> If this method is invoked after the virtual machine has begun its
  79      * shutdown sequence then if shutdown hooks are being run this method will
  80      * block indefinitely.  If shutdown hooks have already been run and on-exit
  81      * finalization has been enabled then this method halts the virtual machine
  82      * with the given status code if the status is nonzero; otherwise, it
  83      * blocks indefinitely.
  84      *
  85      * <p> The <tt>{@link System#exit(int) System.exit}</tt> method is the
  86      * conventional and convenient means of invoking this method. <p>
  87      *
  88      * @param  status
  89      *         Termination status.  By convention, a nonzero status code
  90      *         indicates abnormal termination.
  91      *
  92      * @throws SecurityException
  93      *         If a security manager is present and its <tt>{@link
  94      *         SecurityManager#checkExit checkExit}</tt> method does not permit
  95      *         exiting with the specified status
  96      *
  97      * @see java.lang.SecurityException
  98      * @see java.lang.SecurityManager#checkExit(int)
  99      * @see #addShutdownHook
 100      * @see #removeShutdownHook
 101      * @see #runFinalizersOnExit
 102      * @see #halt(int)
 103      */
 104     public void exit(int status) {
 105         SecurityManager security = System.getSecurityManager();
 106         if (security != null) {
 107             security.checkExit(status);
 108         }
 109         Shutdown.exit(status);
 110     }
 111 
 112     /**
 113      * Registers a new virtual-machine shutdown hook.
 114      *
 115      * <p> The Java virtual machine <i>shuts down</i> in response to two kinds
 116      * of events:
 117      *
 118      *   <ul>
 119      *
 120      *   <li> The program <i>exits</i> normally, when the last non-daemon
 121      *   thread exits or when the <tt>{@link #exit exit}</tt> (equivalently,
 122      *   {@link System#exit(int) System.exit}) method is invoked, or
 123      *
 124      *   <li> The virtual machine is <i>terminated</i> in response to a
 125      *   user interrupt, such as typing <tt>^C</tt>, or a system-wide event,
 126      *   such as user logoff or system shutdown.
 127      *
 128      *   </ul>
 129      *
 130      * <p> A <i>shutdown hook</i> is simply an initialized but unstarted
 131      * thread.  When the virtual machine begins its shutdown sequence it will
 132      * start all registered shutdown hooks in some unspecified order and let
 133      * them run concurrently.  When all the hooks have finished it will then
 134      * run all uninvoked finalizers if finalization-on-exit has been enabled.
 135      * Finally, the virtual machine will halt.  Note that daemon threads will
 136      * continue to run during the shutdown sequence, as will non-daemon threads
 137      * if shutdown was initiated by invoking the <tt>{@link #exit exit}</tt>
 138      * method.
 139      *
 140      * <p> Once the shutdown sequence has begun it can be stopped only by
 141      * invoking the <tt>{@link #halt halt}</tt> method, which forcibly
 142      * terminates the virtual machine.
 143      *
 144      * <p> Once the shutdown sequence has begun it is impossible to register a
 145      * new shutdown hook or de-register a previously-registered hook.
 146      * Attempting either of these operations will cause an
 147      * <tt>{@link IllegalStateException}</tt> to be thrown.
 148      *
 149      * <p> Shutdown hooks run at a delicate time in the life cycle of a virtual
 150      * machine and should therefore be coded defensively.  They should, in
 151      * particular, be written to be thread-safe and to avoid deadlocks insofar
 152      * as possible.  They should also not rely blindly upon services that may
 153      * have registered their own shutdown hooks and therefore may themselves in
 154      * the process of shutting down.  Attempts to use other thread-based
 155      * services such as the AWT event-dispatch thread, for example, may lead to
 156      * deadlocks.
 157      *
 158      * <p> Shutdown hooks should also finish their work quickly.  When a
 159      * program invokes <tt>{@link #exit exit}</tt> the expectation is
 160      * that the virtual machine will promptly shut down and exit.  When the
 161      * virtual machine is terminated due to user logoff or system shutdown the
 162      * underlying operating system may only allow a fixed amount of time in
 163      * which to shut down and exit.  It is therefore inadvisable to attempt any
 164      * user interaction or to perform a long-running computation in a shutdown
 165      * hook.
 166      *
 167      * <p> Uncaught exceptions are handled in shutdown hooks just as in any
 168      * other thread, by invoking the <tt>{@link ThreadGroup#uncaughtException
 169      * uncaughtException}</tt> method of the thread's <tt>{@link
 170      * ThreadGroup}</tt> object.  The default implementation of this method
 171      * prints the exception's stack trace to <tt>{@link System#err}</tt> and
 172      * terminates the thread; it does not cause the virtual machine to exit or
 173      * halt.
 174      *
 175      * <p> In rare circumstances the virtual machine may <i>abort</i>, that is,
 176      * stop running without shutting down cleanly.  This occurs when the
 177      * virtual machine is terminated externally, for example with the
 178      * <tt>SIGKILL</tt> signal on Unix or the <tt>TerminateProcess</tt> call on
 179      * Microsoft Windows.  The virtual machine may also abort if a native
 180      * method goes awry by, for example, corrupting internal data structures or
 181      * attempting to access nonexistent memory.  If the virtual machine aborts
 182      * then no guarantee can be made about whether or not any shutdown hooks
 183      * will be run. <p>
 184      *
 185      * @param   hook
 186      *          An initialized but unstarted <tt>{@link Thread}</tt> object
 187      *
 188      * @throws  IllegalArgumentException
 189      *          If the specified hook has already been registered,
 190      *          or if it can be determined that the hook is already running or
 191      *          has already been run
 192      *
 193      * @throws  IllegalStateException
 194      *          If the virtual machine is already in the process
 195      *          of shutting down
 196      *
 197      * @throws  SecurityException
 198      *          If a security manager is present and it denies
 199      *          <tt>{@link RuntimePermission}("shutdownHooks")</tt>
 200      *
 201      * @see #removeShutdownHook
 202      * @see #halt(int)
 203      * @see #exit(int)
 204      * @since 1.3
 205      */
 206     public void addShutdownHook(Thread hook) {
 207         SecurityManager sm = System.getSecurityManager();
 208         if (sm != null) {
 209             sm.checkPermission(new RuntimePermission("shutdownHooks"));
 210         }
 211         ApplicationShutdownHooks.add(hook);
 212     }
 213 
 214     /**
 215      * De-registers a previously-registered virtual-machine shutdown hook. <p>
 216      *
 217      * @param hook the hook to remove
 218      * @return <tt>true</tt> if the specified hook had previously been
 219      * registered and was successfully de-registered, <tt>false</tt>
 220      * otherwise.
 221      *
 222      * @throws  IllegalStateException
 223      *          If the virtual machine is already in the process of shutting
 224      *          down
 225      *
 226      * @throws  SecurityException
 227      *          If a security manager is present and it denies
 228      *          <tt>{@link RuntimePermission}("shutdownHooks")</tt>
 229      *
 230      * @see #addShutdownHook
 231      * @see #exit(int)
 232      * @since 1.3
 233      */
 234     public boolean removeShutdownHook(Thread hook) {
 235         SecurityManager sm = System.getSecurityManager();
 236         if (sm != null) {
 237             sm.checkPermission(new RuntimePermission("shutdownHooks"));
 238         }
 239         return ApplicationShutdownHooks.remove(hook);
 240     }
 241 
 242     /**
 243      * Forcibly terminates the currently running Java virtual machine.  This
 244      * method never returns normally.
 245      *
 246      * <p> This method should be used with extreme caution.  Unlike the
 247      * <tt>{@link #exit exit}</tt> method, this method does not cause shutdown
 248      * hooks to be started and does not run uninvoked finalizers if
 249      * finalization-on-exit has been enabled.  If the shutdown sequence has
 250      * already been initiated then this method does not wait for any running
 251      * shutdown hooks or finalizers to finish their work. <p>
 252      *
 253      * @param  status
 254      *         Termination status.  By convention, a nonzero status code
 255      *         indicates abnormal termination.  If the <tt>{@link Runtime#exit
 256      *         exit}</tt> (equivalently, <tt>{@link System#exit(int)
 257      *         System.exit}</tt>) method has already been invoked then this
 258      *         status code will override the status code passed to that method.
 259      *
 260      * @throws SecurityException
 261      *         If a security manager is present and its <tt>{@link
 262      *         SecurityManager#checkExit checkExit}</tt> method does not permit
 263      *         an exit with the specified status
 264      *
 265      * @see #exit
 266      * @see #addShutdownHook
 267      * @see #removeShutdownHook
 268      * @since 1.3
 269      */
 270     public void halt(int status) {
 271         SecurityManager sm = System.getSecurityManager();
 272         if (sm != null) {
 273             sm.checkExit(status);
 274         }
 275         Shutdown.beforeHalt();
 276         Shutdown.halt(status);
 277     }
 278 
 279     /**
 280      * Enable or disable finalization on exit; doing so specifies that the
 281      * finalizers of all objects that have finalizers that have not yet been
 282      * automatically invoked are to be run before the Java runtime exits.
 283      * By default, finalization on exit is disabled.
 284      *
 285      * <p>If there is a security manager,
 286      * its <code>checkExit</code> method is first called
 287      * with 0 as its argument to ensure the exit is allowed.
 288      * This could result in a SecurityException.
 289      *
 290      * @param value true to enable finalization on exit, false to disable
 291      * @deprecated  This method is inherently unsafe.  It may result in
 292      *      finalizers being called on live objects while other threads are
 293      *      concurrently manipulating those objects, resulting in erratic
 294      *      behavior or deadlock.
 295      *
 296      * @throws  SecurityException
 297      *        if a security manager exists and its <code>checkExit</code>
 298      *        method doesn't allow the exit.
 299      *
 300      * @see     java.lang.Runtime#exit(int)
 301      * @see     java.lang.Runtime#gc()
 302      * @see     java.lang.SecurityManager#checkExit(int)
 303      * @since   JDK1.1
 304      */
 305     @Deprecated
 306     public static void runFinalizersOnExit(boolean value) {
 307         SecurityManager security = System.getSecurityManager();
 308         if (security != null) {
 309             try {
 310                 security.checkExit(0);
 311             } catch (SecurityException e) {
 312                 throw new SecurityException("runFinalizersOnExit");
 313             }
 314         }
 315         Shutdown.setRunFinalizersOnExit(value);
 316     }
 317 
 318     /**
 319      * Executes the specified string command in a separate process.
 320      *
 321      * <p>This is a convenience method.  An invocation of the form
 322      * <tt>exec(command)</tt>
 323      * behaves in exactly the same way as the invocation
 324      * <tt>{@link #exec(String, String[], File) exec}(command, null, null)</tt>.
 325      *
 326      * @param   command   a specified system command.
 327      *
 328      * @return  A new {@link Process} object for managing the subprocess
 329      *
 330      * @throws  SecurityException
 331      *          If a security manager exists and its
 332      *          {@link SecurityManager#checkExec checkExec}
 333      *          method doesn't allow creation of the subprocess
 334      *
 335      * @throws  IOException
 336      *          If an I/O error occurs
 337      *
 338      * @throws  NullPointerException
 339      *          If <code>command</code> is <code>null</code>
 340      *
 341      * @throws  IllegalArgumentException
 342      *          If <code>command</code> is empty
 343      *
 344      * @see     #exec(String[], String[], File)
 345      * @see     ProcessBuilder
 346      */
 347     public Process exec(String command) throws IOException {
 348         return exec(command, null, null);
 349     }
 350 
 351     /**
 352      * Executes the specified string command in a separate process with the
 353      * specified environment.
 354      *
 355      * <p>This is a convenience method.  An invocation of the form
 356      * <tt>exec(command, envp)</tt>
 357      * behaves in exactly the same way as the invocation
 358      * <tt>{@link #exec(String, String[], File) exec}(command, envp, null)</tt>.
 359      *
 360      * @param   command   a specified system command.
 361      *
 362      * @param   envp      array of strings, each element of which
 363      *                    has environment variable settings in the format
 364      *                    <i>name</i>=<i>value</i>, or
 365      *                    <tt>null</tt> if the subprocess should inherit
 366      *                    the environment of the current process.
 367      *
 368      * @return  A new {@link Process} object for managing the subprocess
 369      *
 370      * @throws  SecurityException
 371      *          If a security manager exists and its
 372      *          {@link SecurityManager#checkExec checkExec}
 373      *          method doesn't allow creation of the subprocess
 374      *
 375      * @throws  IOException
 376      *          If an I/O error occurs
 377      *
 378      * @throws  NullPointerException
 379      *          If <code>command</code> is <code>null</code>,
 380      *          or one of the elements of <code>envp</code> is <code>null</code>
 381      *
 382      * @throws  IllegalArgumentException
 383      *          If <code>command</code> is empty
 384      *
 385      * @see     #exec(String[], String[], File)
 386      * @see     ProcessBuilder
 387      */
 388     public Process exec(String command, String[] envp) throws IOException {
 389         return exec(command, envp, null);
 390     }
 391 
 392     /**
 393      * Executes the specified string command in a separate process with the
 394      * specified environment and working directory.
 395      *
 396      * <p>This is a convenience method.  An invocation of the form
 397      * <tt>exec(command, envp, dir)</tt>
 398      * behaves in exactly the same way as the invocation
 399      * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, envp, dir)</tt>,
 400      * where <code>cmdarray</code> is an array of all the tokens in
 401      * <code>command</code>.
 402      *
 403      * <p>More precisely, the <code>command</code> string is broken
 404      * into tokens using a {@link StringTokenizer} created by the call
 405      * <code>new {@link StringTokenizer}(command)</code> with no
 406      * further modification of the character categories.  The tokens
 407      * produced by the tokenizer are then placed in the new string
 408      * array <code>cmdarray</code>, in the same order.
 409      *
 410      * @param   command   a specified system command.
 411      *
 412      * @param   envp      array of strings, each element of which
 413      *                    has environment variable settings in the format
 414      *                    <i>name</i>=<i>value</i>, or
 415      *                    <tt>null</tt> if the subprocess should inherit
 416      *                    the environment of the current process.
 417      *
 418      * @param   dir       the working directory of the subprocess, or
 419      *                    <tt>null</tt> if the subprocess should inherit
 420      *                    the working directory of the current process.
 421      *
 422      * @return  A new {@link Process} object for managing the subprocess
 423      *
 424      * @throws  SecurityException
 425      *          If a security manager exists and its
 426      *          {@link SecurityManager#checkExec checkExec}
 427      *          method doesn't allow creation of the subprocess
 428      *
 429      * @throws  IOException
 430      *          If an I/O error occurs
 431      *
 432      * @throws  NullPointerException
 433      *          If <code>command</code> is <code>null</code>,
 434      *          or one of the elements of <code>envp</code> is <code>null</code>
 435      *
 436      * @throws  IllegalArgumentException
 437      *          If <code>command</code> is empty
 438      *
 439      * @see     ProcessBuilder
 440      * @since 1.3
 441      */
 442     public Process exec(String command, String[] envp, File dir)
 443         throws IOException {
 444         if (command.length() == 0)
 445             throw new IllegalArgumentException("Empty command");
 446 
 447         StringTokenizer st = new StringTokenizer(command);
 448         String[] cmdarray = new String[st.countTokens()];
 449         for (int i = 0; st.hasMoreTokens(); i++)
 450             cmdarray[i] = st.nextToken();
 451         return exec(cmdarray, envp, dir);
 452     }
 453 
 454     /**
 455      * Executes the specified command and arguments in a separate process.
 456      *
 457      * <p>This is a convenience method.  An invocation of the form
 458      * <tt>exec(cmdarray)</tt>
 459      * behaves in exactly the same way as the invocation
 460      * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, null, null)</tt>.
 461      *
 462      * @param   cmdarray  array containing the command to call and
 463      *                    its arguments.
 464      *
 465      * @return  A new {@link Process} object for managing the subprocess
 466      *
 467      * @throws  SecurityException
 468      *          If a security manager exists and its
 469      *          {@link SecurityManager#checkExec checkExec}
 470      *          method doesn't allow creation of the subprocess
 471      *
 472      * @throws  IOException
 473      *          If an I/O error occurs
 474      *
 475      * @throws  NullPointerException
 476      *          If <code>cmdarray</code> is <code>null</code>,
 477      *          or one of the elements of <code>cmdarray</code> is <code>null</code>
 478      *
 479      * @throws  IndexOutOfBoundsException
 480      *          If <code>cmdarray</code> is an empty array
 481      *          (has length <code>0</code>)
 482      *
 483      * @see     ProcessBuilder
 484      */
 485     public Process exec(String cmdarray[]) throws IOException {
 486         return exec(cmdarray, null, null);
 487     }
 488 
 489     /**
 490      * Executes the specified command and arguments in a separate process
 491      * with the specified environment.
 492      *
 493      * <p>This is a convenience method.  An invocation of the form
 494      * <tt>exec(cmdarray, envp)</tt>
 495      * behaves in exactly the same way as the invocation
 496      * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, envp, null)</tt>.
 497      *
 498      * @param   cmdarray  array containing the command to call and
 499      *                    its arguments.
 500      *
 501      * @param   envp      array of strings, each element of which
 502      *                    has environment variable settings in the format
 503      *                    <i>name</i>=<i>value</i>, or
 504      *                    <tt>null</tt> if the subprocess should inherit
 505      *                    the environment of the current process.
 506      *
 507      * @return  A new {@link Process} object for managing the subprocess
 508      *
 509      * @throws  SecurityException
 510      *          If a security manager exists and its
 511      *          {@link SecurityManager#checkExec checkExec}
 512      *          method doesn't allow creation of the subprocess
 513      *
 514      * @throws  IOException
 515      *          If an I/O error occurs
 516      *
 517      * @throws  NullPointerException
 518      *          If <code>cmdarray</code> is <code>null</code>,
 519      *          or one of the elements of <code>cmdarray</code> is <code>null</code>,
 520      *          or one of the elements of <code>envp</code> is <code>null</code>
 521      *
 522      * @throws  IndexOutOfBoundsException
 523      *          If <code>cmdarray</code> is an empty array
 524      *          (has length <code>0</code>)
 525      *
 526      * @see     ProcessBuilder
 527      */
 528     public Process exec(String[] cmdarray, String[] envp) throws IOException {
 529         return exec(cmdarray, envp, null);
 530     }
 531 
 532 
 533     /**
 534      * Executes the specified command and arguments in a separate process with
 535      * the specified environment and working directory.
 536      *
 537      * <p>Given an array of strings <code>cmdarray</code>, representing the
 538      * tokens of a command line, and an array of strings <code>envp</code>,
 539      * representing "environment" variable settings, this method creates
 540      * a new process in which to execute the specified command.
 541      *
 542      * <p>This method checks that <code>cmdarray</code> is a valid operating
 543      * system command.  Which commands are valid is system-dependent,
 544      * but at the very least the command must be a non-empty list of
 545      * non-null strings.
 546      *
 547      * <p>If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the
 548      * environment settings of the current process.
 549      *
 550      * <p>A minimal set of system dependent environment variables may
 551      * be required to start a process on some operating systems.
 552      * As a result, the subprocess may inherit additional environment variable
 553      * settings beyond those in the specified environment.
 554      *
 555      * <p>{@link ProcessBuilder#start()} is now the preferred way to
 556      * start a process with a modified environment.
 557      *
 558      * <p>The working directory of the new subprocess is specified by <tt>dir</tt>.
 559      * If <tt>dir</tt> is <tt>null</tt>, the subprocess inherits the
 560      * current working directory of the current process.
 561      *
 562      * <p>If a security manager exists, its
 563      * {@link SecurityManager#checkExec checkExec}
 564      * method is invoked with the first component of the array
 565      * <code>cmdarray</code> as its argument. This may result in a
 566      * {@link SecurityException} being thrown.
 567      *
 568      * <p>Starting an operating system process is highly system-dependent.
 569      * Among the many things that can go wrong are:
 570      * <ul>
 571      * <li>The operating system program file was not found.
 572      * <li>Access to the program file was denied.
 573      * <li>The working directory does not exist.
 574      * </ul>
 575      *
 576      * <p>In such cases an exception will be thrown.  The exact nature
 577      * of the exception is system-dependent, but it will always be a
 578      * subclass of {@link IOException}.
 579      *
 580      *
 581      * @param   cmdarray  array containing the command to call and
 582      *                    its arguments.
 583      *
 584      * @param   envp      array of strings, each element of which
 585      *                    has environment variable settings in the format
 586      *                    <i>name</i>=<i>value</i>, or
 587      *                    <tt>null</tt> if the subprocess should inherit
 588      *                    the environment of the current process.
 589      *
 590      * @param   dir       the working directory of the subprocess, or
 591      *                    <tt>null</tt> if the subprocess should inherit
 592      *                    the working directory of the current process.
 593      *
 594      * @return  A new {@link Process} object for managing the subprocess
 595      *
 596      * @throws  SecurityException
 597      *          If a security manager exists and its
 598      *          {@link SecurityManager#checkExec checkExec}
 599      *          method doesn't allow creation of the subprocess
 600      *
 601      * @throws  IOException
 602      *          If an I/O error occurs
 603      *
 604      * @throws  NullPointerException
 605      *          If <code>cmdarray</code> is <code>null</code>,
 606      *          or one of the elements of <code>cmdarray</code> is <code>null</code>,
 607      *          or one of the elements of <code>envp</code> is <code>null</code>
 608      *
 609      * @throws  IndexOutOfBoundsException
 610      *          If <code>cmdarray</code> is an empty array
 611      *          (has length <code>0</code>)
 612      *
 613      * @see     ProcessBuilder
 614      * @since 1.3
 615      */
 616     public Process exec(String[] cmdarray, String[] envp, File dir)
 617         throws IOException {
 618         return new ProcessBuilder(cmdarray)
 619             .environment(envp)
 620             .directory(dir)
 621             .start();
 622     }
 623 
 624     /**
 625      * Returns the number of processors available to the Java virtual machine.
 626      *
 627      * <p> This value may change during a particular invocation of the virtual
 628      * machine.  Applications that are sensitive to the number of available
 629      * processors should therefore occasionally poll this property and adjust
 630      * their resource usage appropriately. </p>
 631      *
 632      * @return  the maximum number of processors available to the virtual
 633      *          machine; never smaller than one
 634      * @since 1.4
 635      */
 636     public native int availableProcessors();
 637 
 638     /**
 639      * Returns the amount of free memory in the Java Virtual Machine.
 640      * Calling the
 641      * <code>gc</code> method may result in increasing the value returned
 642      * by <code>freeMemory.</code>
 643      *
 644      * @return  an approximation to the total amount of memory currently
 645      *          available for future allocated objects, measured in bytes.
 646      */
 647     public native long freeMemory();
 648 
 649     /**
 650      * Returns the total amount of memory in the Java virtual machine.
 651      * The value returned by this method may vary over time, depending on
 652      * the host environment.
 653      * <p>
 654      * Note that the amount of memory required to hold an object of any
 655      * given type may be implementation-dependent.
 656      *
 657      * @return  the total amount of memory currently available for current
 658      *          and future objects, measured in bytes.
 659      */
 660     public native long totalMemory();
 661 
 662     /**
 663      * Returns the maximum amount of memory that the Java virtual machine will
 664      * attempt to use.  If there is no inherent limit then the value {@link
 665      * java.lang.Long#MAX_VALUE} will be returned.
 666      *
 667      * @return  the maximum amount of memory that the virtual machine will
 668      *          attempt to use, measured in bytes
 669      * @since 1.4
 670      */
 671     public native long maxMemory();
 672 
 673     /**
 674      * Runs the garbage collector.
 675      * Calling this method suggests that the Java virtual machine expend
 676      * effort toward recycling unused objects in order to make the memory
 677      * they currently occupy available for quick reuse. When control
 678      * returns from the method call, the virtual machine has made
 679      * its best effort to recycle all discarded objects.
 680      * <p>
 681      * The name <code>gc</code> stands for "garbage
 682      * collector". The virtual machine performs this recycling
 683      * process automatically as needed, in a separate thread, even if the
 684      * <code>gc</code> method is not invoked explicitly.
 685      * <p>
 686      * The method {@link System#gc()} is the conventional and convenient
 687      * means of invoking this method.
 688      */
 689     public native void gc();
 690 
 691     /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */
 692     private static native void runFinalization0();
 693 
 694     /**
 695      * Runs the finalization methods of any objects pending finalization.
 696      * Calling this method suggests that the Java virtual machine expend
 697      * effort toward running the <code>finalize</code> methods of objects
 698      * that have been found to be discarded but whose <code>finalize</code>
 699      * methods have not yet been run. When control returns from the
 700      * method call, the virtual machine has made a best effort to
 701      * complete all outstanding finalizations.
 702      * <p>
 703      * The virtual machine performs the finalization process
 704      * automatically as needed, in a separate thread, if the
 705      * <code>runFinalization</code> method is not invoked explicitly.
 706      * <p>
 707      * The method {@link System#runFinalization()} is the conventional
 708      * and convenient means of invoking this method.
 709      *
 710      * @see     java.lang.Object#finalize()
 711      */
 712     public void runFinalization() {
 713         runFinalization0();
 714     }
 715 
 716     /**
 717      * Enables/Disables tracing of instructions.
 718      * If the <code>boolean</code> argument is <code>true</code>, this
 719      * method suggests that the Java virtual machine emit debugging
 720      * information for each instruction in the virtual machine as it
 721      * is executed. The format of this information, and the file or other
 722      * output stream to which it is emitted, depends on the host environment.
 723      * The virtual machine may ignore this request if it does not support
 724      * this feature. The destination of the trace output is system
 725      * dependent.
 726      * <p>
 727      * If the <code>boolean</code> argument is <code>false</code>, this
 728      * method causes the virtual machine to stop performing the
 729      * detailed instruction trace it is performing.
 730      *
 731      * @param   on   <code>true</code> to enable instruction tracing;
 732      *               <code>false</code> to disable this feature.
 733      */
 734     public native void traceInstructions(boolean on);
 735 
 736     /**
 737      * Enables/Disables tracing of method calls.
 738      * If the <code>boolean</code> argument is <code>true</code>, this
 739      * method suggests that the Java virtual machine emit debugging
 740      * information for each method in the virtual machine as it is
 741      * called. The format of this information, and the file or other output
 742      * stream to which it is emitted, depends on the host environment. The
 743      * virtual machine may ignore this request if it does not support
 744      * this feature.
 745      * <p>
 746      * Calling this method with argument false suggests that the
 747      * virtual machine cease emitting per-call debugging information.
 748      *
 749      * @param   on   <code>true</code> to enable instruction tracing;
 750      *               <code>false</code> to disable this feature.
 751      */
 752     public native void traceMethodCalls(boolean on);
 753 
 754     /**
 755      * Loads the native library specified by the filename argument.  The filename
 756      * argument must be an absolute path name.
 757      * (for example
 758      * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>).
 759      *
 760      * If the filename argument, when stripped of any platform-specific library
 761      * prefix, path, and file extension, indicates a library whose name is,
 762      * for example, L, and a native library called L is statically linked
 763      * with the VM, then the JNI_OnLoad_L function exported by the library
 764      * is invoked rather than attempting to load a dynamic library.
 765      * A filename matching the argument does not have to exist in the file
 766      * system. See the JNI Specification for more details.
 767      *
 768      * Otherwise, the filename argument is mapped to a native library image in
 769      * an implementation-dependent manner.
 770      * <p>
 771      * First, if there is a security manager, its <code>checkLink</code>
 772      * method is called with the <code>filename</code> as its argument.
 773      * This may result in a security exception.
 774      * <p>
 775      * This is similar to the method {@link #loadLibrary(String)}, but it
 776      * accepts a general file name as an argument rather than just a library
 777      * name, allowing any file of native code to be loaded.
 778      * <p>
 779      * The method {@link System#load(String)} is the conventional and
 780      * convenient means of invoking this method.
 781      *
 782      * @param      filename   the file to load.
 783      * @exception  SecurityException  if a security manager exists and its
 784      *             <code>checkLink</code> method doesn't allow
 785      *             loading of the specified dynamic library
 786      * @exception  UnsatisfiedLinkError  if either the filename is not an
 787      *             absolute path name, the native library is not statically
 788      *             linked with the VM, or the library cannot be mapped to
 789      *             a native library image by the host system.
 790      * @exception  NullPointerException if <code>filename</code> is
 791      *             <code>null</code>
 792      * @see        java.lang.Runtime#getRuntime()
 793      * @see        java.lang.SecurityException
 794      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 795      */
 796     @CallerSensitive
 797     public void load(String filename) {
 798         load0(Reflection.getCallerClass(), filename);
 799     }
 800 
 801     synchronized void load0(Class<?> fromClass, String filename) {
 802         SecurityManager security = System.getSecurityManager();
 803         if (security != null) {
 804             security.checkLink(filename);
 805         }
 806         if (!(new File(filename).isAbsolute())) {
 807             throw new UnsatisfiedLinkError(
 808                 "Expecting an absolute path of the library: " + filename);
 809         }
 810         ClassLoader.loadLibrary(fromClass, filename, true);
 811     }
 812 
 813     /**
 814      * Loads the native library specified by the <code>libname</code>
 815      * argument.  The <code>libname</code> argument must not contain any platform
 816      * specific prefix, file extension or path. If a native library
 817      * called <code>libname</code> is statically linked with the VM, then the
 818      * JNI_OnLoad_<code>libname</code> function exported by the library is invoked.
 819      * See the JNI Specification for more details.
 820      *
 821      * Otherwise, the libname argument is loaded from a system library
 822      * location and mapped to a native library image in an implementation-
 823      * dependent manner.
 824      * <p>
 825      * First, if there is a security manager, its <code>checkLink</code>
 826      * method is called with the <code>libname</code> as its argument.
 827      * This may result in a security exception.
 828      * <p>
 829      * The method {@link System#loadLibrary(String)} is the conventional
 830      * and convenient means of invoking this method. If native
 831      * methods are to be used in the implementation of a class, a standard
 832      * strategy is to put the native code in a library file (call it
 833      * <code>LibFile</code>) and then to put a static initializer:
 834      * <blockquote><pre>
 835      * static { System.loadLibrary("LibFile"); }
 836      * </pre></blockquote>
 837      * within the class declaration. When the class is loaded and
 838      * initialized, the necessary native code implementation for the native
 839      * methods will then be loaded as well.
 840      * <p>
 841      * If this method is called more than once with the same library
 842      * name, the second and subsequent calls are ignored.
 843      *
 844      * @param      libname   the name of the library.
 845      * @exception  SecurityException  if a security manager exists and its
 846      *             <code>checkLink</code> method doesn't allow
 847      *             loading of the specified dynamic library
 848      * @exception  UnsatisfiedLinkError if either the libname argument
 849      *             contains a file path, the native library is not statically
 850      *             linked with the VM,  or the library cannot be mapped to a
 851      *             native library image by the host system.
 852      * @exception  NullPointerException if <code>libname</code> is
 853      *             <code>null</code>
 854      * @see        java.lang.SecurityException
 855      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 856      */
 857     @CallerSensitive
 858     public void loadLibrary(String libname) {
 859         loadLibrary0(Reflection.getCallerClass(), libname);
 860     }
 861 
 862     synchronized void loadLibrary0(Class<?> fromClass, String libname) {
 863         SecurityManager security = System.getSecurityManager();
 864         if (security != null) {
 865             security.checkLink(libname);
 866         }
 867         if (libname.indexOf((int)File.separatorChar) != -1) {
 868             throw new UnsatisfiedLinkError(
 869     "Directory separator should not appear in library name: " + libname);
 870         }
 871         ClassLoader.loadLibrary(fromClass, libname, false);
 872     }
 873 
 874     /**
 875      * Creates a localized version of an input stream. This method takes
 876      * an <code>InputStream</code> and returns an <code>InputStream</code>
 877      * equivalent to the argument in all respects except that it is
 878      * localized: as characters in the local character set are read from
 879      * the stream, they are automatically converted from the local
 880      * character set to Unicode.
 881      * <p>
 882      * If the argument is already a localized stream, it may be returned
 883      * as the result.
 884      *
 885      * @param      in InputStream to localize
 886      * @return     a localized input stream
 887      * @see        java.io.InputStream
 888      * @see        java.io.BufferedReader#BufferedReader(java.io.Reader)
 889      * @see        java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
 890      * @deprecated As of JDK&nbsp;1.1, the preferred way to translate a byte
 891      * stream in the local encoding into a character stream in Unicode is via
 892      * the <code>InputStreamReader</code> and <code>BufferedReader</code>
 893      * classes.
 894      */
 895     @Deprecated
 896     public InputStream getLocalizedInputStream(InputStream in) {
 897         return in;
 898     }
 899 
 900     /**
 901      * Creates a localized version of an output stream. This method
 902      * takes an <code>OutputStream</code> and returns an
 903      * <code>OutputStream</code> equivalent to the argument in all respects
 904      * except that it is localized: as Unicode characters are written to
 905      * the stream, they are automatically converted to the local
 906      * character set.
 907      * <p>
 908      * If the argument is already a localized stream, it may be returned
 909      * as the result.
 910      *
 911      * @deprecated As of JDK&nbsp;1.1, the preferred way to translate a
 912      * Unicode character stream into a byte stream in the local encoding is via
 913      * the <code>OutputStreamWriter</code>, <code>BufferedWriter</code>, and
 914      * <code>PrintWriter</code> classes.
 915      *
 916      * @param      out OutputStream to localize
 917      * @return     a localized output stream
 918      * @see        java.io.OutputStream
 919      * @see        java.io.BufferedWriter#BufferedWriter(java.io.Writer)
 920      * @see        java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
 921      * @see        java.io.PrintWriter#PrintWriter(java.io.OutputStream)
 922      */
 923     @Deprecated
 924     public OutputStream getLocalizedOutputStream(OutputStream out) {
 925         return out;
 926     }
 927 
 928 }