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

Save This Page
Home » openjdk-7 » java » lang » [javadoc | source]