1 /* 2 * Copyright (c) 1995, 2014, 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 1.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. 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. 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. 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.halt(status); 276 } 277 278 /** 279 * Enable or disable finalization on exit; doing so specifies that the 280 * finalizers of all objects that have finalizers that have not yet been 281 * automatically invoked are to be run before the Java runtime exits. 282 * By default, finalization on exit is disabled. 283 * 284 * <p>If there is a security manager, 285 * its <code>checkExit</code> method is first called 286 * with 0 as its argument to ensure the exit is allowed. 287 * This could result in a SecurityException. 288 * 289 * @param value true to enable finalization on exit, false to disable 290 * @deprecated This method is inherently unsafe. It may result in 291 * finalizers being called on live objects while other threads are 292 * concurrently manipulating those objects, resulting in erratic 293 * behavior or deadlock. 294 * 295 * @throws SecurityException 296 * if a security manager exists and its <code>checkExit</code> 297 * method doesn't allow the exit. 298 * 299 * @see java.lang.Runtime#exit(int) 300 * @see java.lang.Runtime#gc() 301 * @see java.lang.SecurityManager#checkExit(int) 302 * @since 1.1 303 */ 304 @Deprecated 305 public static void runFinalizersOnExit(boolean value) { 306 SecurityManager security = System.getSecurityManager(); 307 if (security != null) { 308 try { 309 security.checkExit(0); 310 } catch (SecurityException e) { 311 throw new SecurityException("runFinalizersOnExit"); 312 } 313 } 314 Shutdown.setRunFinalizersOnExit(value); 315 } 316 317 /** 318 * Executes the specified string command in a separate process. 319 * 320 * <p>This is a convenience method. An invocation of the form 321 * <tt>exec(command)</tt> 322 * behaves in exactly the same way as the invocation 323 * <tt>{@link #exec(String, String[], File) exec}(command, null, null)</tt>. 324 * 325 * @param command a specified system command. 326 * 327 * @return A new {@link Process} object for managing the subprocess 328 * 329 * @throws SecurityException 330 * If a security manager exists and its 331 * {@link SecurityManager#checkExec checkExec} 332 * method doesn't allow creation of the subprocess 333 * 334 * @throws IOException 335 * If an I/O error occurs 336 * 337 * @throws NullPointerException 338 * If <code>command</code> is <code>null</code> 339 * 340 * @throws IllegalArgumentException 341 * If <code>command</code> is empty 342 * 343 * @see #exec(String[], String[], File) 344 * @see ProcessBuilder 345 */ 346 public Process exec(String command) throws IOException { 347 return exec(command, null, null); 348 } 349 350 /** 351 * Executes the specified string command in a separate process with the 352 * specified environment. 353 * 354 * <p>This is a convenience method. An invocation of the form 355 * <tt>exec(command, envp)</tt> 356 * behaves in exactly the same way as the invocation 357 * <tt>{@link #exec(String, String[], File) exec}(command, envp, null)</tt>. 358 * 359 * @param command a specified system command. 360 * 361 * @param envp array of strings, each element of which 362 * has environment variable settings in the format 363 * <i>name</i>=<i>value</i>, or 364 * <tt>null</tt> if the subprocess should inherit 365 * the environment of the current process. 366 * 367 * @return A new {@link Process} object for managing the subprocess 368 * 369 * @throws SecurityException 370 * If a security manager exists and its 371 * {@link SecurityManager#checkExec checkExec} 372 * method doesn't allow creation of the subprocess 373 * 374 * @throws IOException 375 * If an I/O error occurs 376 * 377 * @throws NullPointerException 378 * If <code>command</code> is <code>null</code>, 379 * or one of the elements of <code>envp</code> is <code>null</code> 380 * 381 * @throws IllegalArgumentException 382 * If <code>command</code> is empty 383 * 384 * @see #exec(String[], String[], File) 385 * @see ProcessBuilder 386 */ 387 public Process exec(String command, String[] envp) throws IOException { 388 return exec(command, envp, null); 389 } 390 391 /** 392 * Executes the specified string command in a separate process with the 393 * specified environment and working directory. 394 * 395 * <p>This is a convenience method. An invocation of the form 396 * <tt>exec(command, envp, dir)</tt> 397 * behaves in exactly the same way as the invocation 398 * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, envp, dir)</tt>, 399 * where <code>cmdarray</code> is an array of all the tokens in 400 * <code>command</code>. 401 * 402 * <p>More precisely, the <code>command</code> string is broken 403 * into tokens using a {@link StringTokenizer} created by the call 404 * <code>new {@link StringTokenizer}(command)</code> with no 405 * further modification of the character categories. The tokens 406 * produced by the tokenizer are then placed in the new string 407 * array <code>cmdarray</code>, in the same order. 408 * 409 * @param command a specified system command. 410 * 411 * @param envp array of strings, each element of which 412 * has environment variable settings in the format 413 * <i>name</i>=<i>value</i>, or 414 * <tt>null</tt> if the subprocess should inherit 415 * the environment of the current process. 416 * 417 * @param dir the working directory of the subprocess, or 418 * <tt>null</tt> if the subprocess should inherit 419 * the working directory of the current process. 420 * 421 * @return A new {@link Process} object for managing the subprocess 422 * 423 * @throws SecurityException 424 * If a security manager exists and its 425 * {@link SecurityManager#checkExec checkExec} 426 * method doesn't allow creation of the subprocess 427 * 428 * @throws IOException 429 * If an I/O error occurs 430 * 431 * @throws NullPointerException 432 * If <code>command</code> is <code>null</code>, 433 * or one of the elements of <code>envp</code> is <code>null</code> 434 * 435 * @throws IllegalArgumentException 436 * If <code>command</code> is empty 437 * 438 * @see ProcessBuilder 439 * @since 1.3 440 */ 441 public Process exec(String command, String[] envp, File dir) 442 throws IOException { 443 if (command.length() == 0) 444 throw new IllegalArgumentException("Empty command"); 445 446 StringTokenizer st = new StringTokenizer(command); 447 String[] cmdarray = new String[st.countTokens()]; 448 for (int i = 0; st.hasMoreTokens(); i++) 449 cmdarray[i] = st.nextToken(); 450 return exec(cmdarray, envp, dir); 451 } 452 453 /** 454 * Executes the specified command and arguments in a separate process. 455 * 456 * <p>This is a convenience method. An invocation of the form 457 * <tt>exec(cmdarray)</tt> 458 * behaves in exactly the same way as the invocation 459 * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, null, null)</tt>. 460 * 461 * @param cmdarray array containing the command to call and 462 * its arguments. 463 * 464 * @return A new {@link Process} object for managing the subprocess 465 * 466 * @throws SecurityException 467 * If a security manager exists and its 468 * {@link SecurityManager#checkExec checkExec} 469 * method doesn't allow creation of the subprocess 470 * 471 * @throws IOException 472 * If an I/O error occurs 473 * 474 * @throws NullPointerException 475 * If <code>cmdarray</code> is <code>null</code>, 476 * or one of the elements of <code>cmdarray</code> is <code>null</code> 477 * 478 * @throws IndexOutOfBoundsException 479 * If <code>cmdarray</code> is an empty array 480 * (has length <code>0</code>) 481 * 482 * @see ProcessBuilder 483 */ 484 public Process exec(String cmdarray[]) throws IOException { 485 return exec(cmdarray, null, null); 486 } 487 488 /** 489 * Executes the specified command and arguments in a separate process 490 * with the specified environment. 491 * 492 * <p>This is a convenience method. An invocation of the form 493 * <tt>exec(cmdarray, envp)</tt> 494 * behaves in exactly the same way as the invocation 495 * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, envp, null)</tt>. 496 * 497 * @param cmdarray array containing the command to call and 498 * its arguments. 499 * 500 * @param envp array of strings, each element of which 501 * has environment variable settings in the format 502 * <i>name</i>=<i>value</i>, or 503 * <tt>null</tt> if the subprocess should inherit 504 * the environment of the current process. 505 * 506 * @return A new {@link Process} object for managing the subprocess 507 * 508 * @throws SecurityException 509 * If a security manager exists and its 510 * {@link SecurityManager#checkExec checkExec} 511 * method doesn't allow creation of the subprocess 512 * 513 * @throws IOException 514 * If an I/O error occurs 515 * 516 * @throws NullPointerException 517 * If <code>cmdarray</code> is <code>null</code>, 518 * or one of the elements of <code>cmdarray</code> is <code>null</code>, 519 * or one of the elements of <code>envp</code> is <code>null</code> 520 * 521 * @throws IndexOutOfBoundsException 522 * If <code>cmdarray</code> is an empty array 523 * (has length <code>0</code>) 524 * 525 * @see ProcessBuilder 526 */ 527 public Process exec(String[] cmdarray, String[] envp) throws IOException { 528 return exec(cmdarray, envp, null); 529 } 530 531 532 /** 533 * Executes the specified command and arguments in a separate process with 534 * the specified environment and working directory. 535 * 536 * <p>Given an array of strings <code>cmdarray</code>, representing the 537 * tokens of a command line, and an array of strings <code>envp</code>, 538 * representing "environment" variable settings, this method creates 539 * a new process in which to execute the specified command. 540 * 541 * <p>This method checks that <code>cmdarray</code> is a valid operating 542 * system command. Which commands are valid is system-dependent, 543 * but at the very least the command must be a non-empty list of 544 * non-null strings. 545 * 546 * <p>If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the 547 * environment settings of the current process. 548 * 549 * <p>A minimal set of system dependent environment variables may 550 * be required to start a process on some operating systems. 551 * As a result, the subprocess may inherit additional environment variable 552 * settings beyond those in the specified environment. 553 * 554 * <p>{@link ProcessBuilder#start()} is now the preferred way to 555 * start a process with a modified environment. 556 * 557 * <p>The working directory of the new subprocess is specified by <tt>dir</tt>. 558 * If <tt>dir</tt> is <tt>null</tt>, the subprocess inherits the 559 * current working directory of the current process. 560 * 561 * <p>If a security manager exists, its 562 * {@link SecurityManager#checkExec checkExec} 563 * method is invoked with the first component of the array 564 * <code>cmdarray</code> as its argument. This may result in a 565 * {@link SecurityException} being thrown. 566 * 567 * <p>Starting an operating system process is highly system-dependent. 568 * Among the many things that can go wrong are: 569 * <ul> 570 * <li>The operating system program file was not found. 571 * <li>Access to the program file was denied. 572 * <li>The working directory does not exist. 573 * </ul> 574 * 575 * <p>In such cases an exception will be thrown. The exact nature 576 * of the exception is system-dependent, but it will always be a 577 * subclass of {@link IOException}. 578 * 579 * 580 * @param cmdarray array containing the command to call and 581 * its arguments. 582 * 583 * @param envp array of strings, each element of which 584 * has environment variable settings in the format 585 * <i>name</i>=<i>value</i>, or 586 * <tt>null</tt> if the subprocess should inherit 587 * the environment of the current process. 588 * 589 * @param dir the working directory of the subprocess, or 590 * <tt>null</tt> if the subprocess should inherit 591 * the working directory of the current process. 592 * 593 * @return A new {@link Process} object for managing the subprocess 594 * 595 * @throws SecurityException 596 * If a security manager exists and its 597 * {@link SecurityManager#checkExec checkExec} 598 * method doesn't allow creation of the subprocess 599 * 600 * @throws IOException 601 * If an I/O error occurs 602 * 603 * @throws NullPointerException 604 * If <code>cmdarray</code> is <code>null</code>, 605 * or one of the elements of <code>cmdarray</code> is <code>null</code>, 606 * or one of the elements of <code>envp</code> is <code>null</code> 607 * 608 * @throws IndexOutOfBoundsException 609 * If <code>cmdarray</code> is an empty array 610 * (has length <code>0</code>) 611 * 612 * @see ProcessBuilder 613 * @since 1.3 614 */ 615 public Process exec(String[] cmdarray, String[] envp, File dir) 616 throws IOException { 617 return new ProcessBuilder(cmdarray) 618 .environment(envp) 619 .directory(dir) 620 .start(); 621 } 622 623 /** 624 * Returns the number of processors available to the Java virtual machine. 625 * 626 * <p> This value may change during a particular invocation of the virtual 627 * machine. Applications that are sensitive to the number of available 628 * processors should therefore occasionally poll this property and adjust 629 * their resource usage appropriately. </p> 630 * 631 * @return the maximum number of processors available to the virtual 632 * machine; never smaller than one 633 * @since 1.4 634 */ 635 public native int availableProcessors(); 636 637 /** 638 * Returns the amount of free memory in the Java Virtual Machine. 639 * Calling the 640 * <code>gc</code> method may result in increasing the value returned 641 * by <code>freeMemory.</code> 642 * 643 * @return an approximation to the total amount of memory currently 644 * available for future allocated objects, measured in bytes. 645 */ 646 public native long freeMemory(); 647 648 /** 649 * Returns the total amount of memory in the Java virtual machine. 650 * The value returned by this method may vary over time, depending on 651 * the host environment. 652 * <p> 653 * Note that the amount of memory required to hold an object of any 654 * given type may be implementation-dependent. 655 * 656 * @return the total amount of memory currently available for current 657 * and future objects, measured in bytes. 658 */ 659 public native long totalMemory(); 660 661 /** 662 * Returns the maximum amount of memory that the Java virtual machine will 663 * attempt to use. If there is no inherent limit then the value {@link 664 * java.lang.Long#MAX_VALUE} will be returned. 665 * 666 * @return the maximum amount of memory that the virtual machine will 667 * attempt to use, measured in bytes 668 * @since 1.4 669 */ 670 public native long maxMemory(); 671 672 /** 673 * Runs the garbage collector. 674 * Calling this method suggests that the Java virtual machine expend 675 * effort toward recycling unused objects in order to make the memory 676 * they currently occupy available for quick reuse. When control 677 * returns from the method call, the virtual machine has made 678 * its best effort to recycle all discarded objects. 679 * <p> 680 * The name <code>gc</code> stands for "garbage 681 * collector". The virtual machine performs this recycling 682 * process automatically as needed, in a separate thread, even if the 683 * <code>gc</code> method is not invoked explicitly. 684 * <p> 685 * The method {@link System#gc()} is the conventional and convenient 686 * means of invoking this method. 687 */ 688 public native void gc(); 689 690 /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */ 691 private static native void runFinalization0(); 692 693 /** 694 * Runs the finalization methods of any objects pending finalization. 695 * Calling this method suggests that the Java virtual machine expend 696 * effort toward running the <code>finalize</code> methods of objects 697 * that have been found to be discarded but whose <code>finalize</code> 698 * methods have not yet been run. When control returns from the 699 * method call, the virtual machine has made a best effort to 700 * complete all outstanding finalizations. 701 * <p> 702 * The virtual machine performs the finalization process 703 * automatically as needed, in a separate thread, if the 704 * <code>runFinalization</code> method is not invoked explicitly. 705 * <p> 706 * The method {@link System#runFinalization()} is the conventional 707 * and convenient means of invoking this method. 708 * 709 * @see java.lang.Object#finalize() 710 */ 711 public void runFinalization() { 712 runFinalization0(); 713 } 714 715 /** 716 * Enables/Disables tracing of instructions. 717 * If the <code>boolean</code> argument is <code>true</code>, this 718 * method suggests that the Java virtual machine emit debugging 719 * information for each instruction in the virtual machine as it 720 * is executed. The format of this information, and the file or other 721 * output stream to which it is emitted, depends on the host environment. 722 * The virtual machine may ignore this request if it does not support 723 * this feature. The destination of the trace output is system 724 * dependent. 725 * <p> 726 * If the <code>boolean</code> argument is <code>false</code>, this 727 * method causes the virtual machine to stop performing the 728 * detailed instruction trace it is performing. 729 * 730 * @param on <code>true</code> to enable instruction tracing; 731 * <code>false</code> to disable this feature. 732 */ 733 public native void traceInstructions(boolean on); 734 735 /** 736 * Enables/Disables tracing of method calls. 737 * If the <code>boolean</code> argument is <code>true</code>, this 738 * method suggests that the Java virtual machine emit debugging 739 * information for each method in the virtual machine as it is 740 * called. The format of this information, and the file or other output 741 * stream to which it is emitted, depends on the host environment. The 742 * virtual machine may ignore this request if it does not support 743 * this feature. 744 * <p> 745 * Calling this method with argument false suggests that the 746 * virtual machine cease emitting per-call debugging information. 747 * 748 * @param on <code>true</code> to enable instruction tracing; 749 * <code>false</code> to disable this feature. 750 */ 751 public native void traceMethodCalls(boolean on); 752 753 /** 754 * Loads the native library specified by the filename argument. The filename 755 * argument must be an absolute path name. 756 * (for example 757 * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>). 758 * 759 * If the filename argument, when stripped of any platform-specific library 760 * prefix, path, and file extension, indicates a library whose name is, 761 * for example, L, and a native library called L is statically linked 762 * with the VM, then the JNI_OnLoad_L function exported by the library 763 * is invoked rather than attempting to load a dynamic library. 764 * A filename matching the argument does not have to exist in the file 765 * system. See the JNI Specification for more details. 766 * 767 * Otherwise, the filename argument is mapped to a native library image in 768 * an implementation-dependent manner. 769 * <p> 770 * First, if there is a security manager, its <code>checkLink</code> 771 * method is called with the <code>filename</code> as its argument. 772 * This may result in a security exception. 773 * <p> 774 * This is similar to the method {@link #loadLibrary(String)}, but it 775 * accepts a general file name as an argument rather than just a library 776 * name, allowing any file of native code to be loaded. 777 * <p> 778 * The method {@link System#load(String)} is the conventional and 779 * convenient means of invoking this method. 780 * 781 * @param filename the file to load. 782 * @exception SecurityException if a security manager exists and its 783 * <code>checkLink</code> method doesn't allow 784 * loading of the specified dynamic library 785 * @exception UnsatisfiedLinkError if either the filename is not an 786 * absolute path name, the native library is not statically 787 * linked with the VM, or the library cannot be mapped to 788 * a native library image by the host system. 789 * @exception NullPointerException if <code>filename</code> is 790 * <code>null</code> 791 * @see java.lang.Runtime#getRuntime() 792 * @see java.lang.SecurityException 793 * @see java.lang.SecurityManager#checkLink(java.lang.String) 794 */ 795 @CallerSensitive 796 public void load(String filename) { 797 load0(Reflection.getCallerClass(), filename); 798 } 799 800 synchronized void load0(Class<?> fromClass, String filename) { 801 SecurityManager security = System.getSecurityManager(); 802 if (security != null) { 803 security.checkLink(filename); 804 } 805 if (!(new File(filename).isAbsolute())) { 806 throw new UnsatisfiedLinkError( 807 "Expecting an absolute path of the library: " + filename); 808 } 809 ClassLoader.loadLibrary(fromClass, filename, true); 810 } 811 812 /** 813 * Loads the native library specified by the <code>libname</code> 814 * argument. The <code>libname</code> argument must not contain any platform 815 * specific prefix, file extension or path. If a native library 816 * called <code>libname</code> is statically linked with the VM, then the 817 * JNI_OnLoad_<code>libname</code> function exported by the library is invoked. 818 * See the JNI Specification for more details. 819 * 820 * Otherwise, the libname argument is loaded from a system library 821 * location and mapped to a native library image in an implementation- 822 * dependent manner. 823 * <p> 824 * First, if there is a security manager, its <code>checkLink</code> 825 * method is called with the <code>libname</code> as its argument. 826 * This may result in a security exception. 827 * <p> 828 * The method {@link System#loadLibrary(String)} is the conventional 829 * and convenient means of invoking this method. If native 830 * methods are to be used in the implementation of a class, a standard 831 * strategy is to put the native code in a library file (call it 832 * <code>LibFile</code>) and then to put a static initializer: 833 * <blockquote><pre> 834 * static { System.loadLibrary("LibFile"); } 835 * </pre></blockquote> 836 * within the class declaration. When the class is loaded and 837 * initialized, the necessary native code implementation for the native 838 * methods will then be loaded as well. 839 * <p> 840 * If this method is called more than once with the same library 841 * name, the second and subsequent calls are ignored. 842 * 843 * @param libname the name of the library. 844 * @exception SecurityException if a security manager exists and its 845 * <code>checkLink</code> method doesn't allow 846 * loading of the specified dynamic library 847 * @exception UnsatisfiedLinkError if either the libname argument 848 * contains a file path, the native library is not statically 849 * linked with the VM, or the library cannot be mapped to a 850 * native library image by the host system. 851 * @exception NullPointerException if <code>libname</code> is 852 * <code>null</code> 853 * @see java.lang.SecurityException 854 * @see java.lang.SecurityManager#checkLink(java.lang.String) 855 */ 856 @CallerSensitive 857 public void loadLibrary(String libname) { 858 loadLibrary0(Reflection.getCallerClass(), libname); 859 } 860 861 synchronized void loadLibrary0(Class<?> fromClass, String libname) { 862 SecurityManager security = System.getSecurityManager(); 863 if (security != null) { 864 security.checkLink(libname); 865 } 866 if (libname.indexOf((int)File.separatorChar) != -1) { 867 throw new UnsatisfiedLinkError( 868 "Directory separator should not appear in library name: " + libname); 869 } 870 ClassLoader.loadLibrary(fromClass, libname, false); 871 } 872 873 /** 874 * Creates a localized version of an input stream. This method takes 875 * an <code>InputStream</code> and returns an <code>InputStream</code> 876 * equivalent to the argument in all respects except that it is 877 * localized: as characters in the local character set are read from 878 * the stream, they are automatically converted from the local 879 * character set to Unicode. 880 * <p> 881 * If the argument is already a localized stream, it may be returned 882 * as the result. 883 * 884 * @param in InputStream to localize 885 * @return a localized input stream 886 * @see java.io.InputStream 887 * @see java.io.BufferedReader#BufferedReader(java.io.Reader) 888 * @see java.io.InputStreamReader#InputStreamReader(java.io.InputStream) 889 * @deprecated As of JDK 1.1, the preferred way to translate a byte 890 * stream in the local encoding into a character stream in Unicode is via 891 * the <code>InputStreamReader</code> and <code>BufferedReader</code> 892 * classes. 893 */ 894 @Deprecated 895 public InputStream getLocalizedInputStream(InputStream in) { 896 return in; 897 } 898 899 /** 900 * Creates a localized version of an output stream. This method 901 * takes an <code>OutputStream</code> and returns an 902 * <code>OutputStream</code> equivalent to the argument in all respects 903 * except that it is localized: as Unicode characters are written to 904 * the stream, they are automatically converted to the local 905 * character set. 906 * <p> 907 * If the argument is already a localized stream, it may be returned 908 * as the result. 909 * 910 * @deprecated As of JDK 1.1, the preferred way to translate a 911 * Unicode character stream into a byte stream in the local encoding is via 912 * the <code>OutputStreamWriter</code>, <code>BufferedWriter</code>, and 913 * <code>PrintWriter</code> classes. 914 * 915 * @param out OutputStream to localize 916 * @return a localized output stream 917 * @see java.io.OutputStream 918 * @see java.io.BufferedWriter#BufferedWriter(java.io.Writer) 919 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) 920 * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream) 921 */ 922 @Deprecated 923 public OutputStream getLocalizedOutputStream(OutputStream out) { 924 return out; 925 } 926 927 }