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