1. /*
  2. * @(#)Runtime.java 1.67 03/01/23
  3. *
  4. * Copyright 2003 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.67, 01/23/03
  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. public static void runFinalizersOnExit(boolean value) {
  273. SecurityManager security = System.getSecurityManager();
  274. if (security != null) {
  275. try {
  276. security.checkExit(0);
  277. } catch (SecurityException e) {
  278. throw new SecurityException("runFinalizersOnExit");
  279. }
  280. }
  281. Shutdown.setRunFinalizersOnExit(value);
  282. }
  283. /* Helper for exec
  284. */
  285. private native Process execInternal(String cmdarray[], String envp[], String path)
  286. throws IOException;
  287. /**
  288. * Executes the specified string command in a separate process.
  289. * <p>
  290. * The <code>command</code> argument is parsed into tokens and then
  291. * executed as a command in a separate process. The token parsing is
  292. * done by a {@link java.util.StringTokenizer} created by the call:
  293. * <blockquote><pre>
  294. * new StringTokenizer(command)
  295. * </pre></blockquote>
  296. * with no further modifications of the character categories.
  297. * This method has exactly the same effect as
  298. * <code>exec(command, null)</code>.
  299. *
  300. * @param command a specified system command.
  301. * @return a <code>Process</code> object for managing the subprocess.
  302. * @exception SecurityException if a security manager exists and its
  303. * <code>checkExec</code> method doesn't allow creation of a subprocess.
  304. * @exception IOException if an I/O error occurs
  305. * @exception NullPointerException if <code>command</code> is
  306. * <code>null</code>
  307. * @exception IllegalArgumentException if <code>command</code> is empty
  308. *
  309. * @see java.lang.Runtime#exec(java.lang.String, java.lang.String[])
  310. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  311. */
  312. public Process exec(String command) throws IOException {
  313. return exec(command, null);
  314. }
  315. /**
  316. * Executes the specified string command in a separate process with the
  317. * specified environment.
  318. * <p>
  319. * This method breaks the <code>command</code> string into tokens and
  320. * creates a new array <code>cmdarray</code> containing the tokens in the
  321. * order that they were produced by the string tokenizer; it
  322. * then performs the call <code>exec(cmdarray, envp)</code>. The token
  323. * parsing is done by a {@link java.util.StringTokenizer} created by
  324. * the call:
  325. * <blockquote><pre>
  326. * new StringTokenizer(command)
  327. * </pre></blockquote>
  328. * with no further modification of the character categories.
  329. *
  330. * <p>
  331. * The environment variable settings are specified by <tt>envp</tt>.
  332. * If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the
  333. * environment settings of the current process.
  334. *
  335. * @param cmd a specified system command.
  336. * @param envp array of strings, each element of which
  337. * has environment variable settings in format
  338. * <i>name</i>=<i>value</i>.
  339. * @return a <code>Process</code> object for managing the subprocess.
  340. * @exception SecurityException if a security manager exists and its
  341. * <code>checkExec</code> method doesn't allow creation of a subprocess.
  342. * @exception IOException if an I/O error occurs
  343. * @exception NullPointerException if <code>cmd</code> is null
  344. * @exception IllegalArgumentException if <code>cmd</code> is empty
  345. * @see java.lang.Runtime#exec(java.lang.String[])
  346. * @see java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  347. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  348. */
  349. public Process exec(String cmd, String envp[]) throws IOException {
  350. return exec(cmd, envp, null);
  351. }
  352. /**
  353. * Executes the specified string command in a separate process with the
  354. * specified environment and working directory.
  355. * <p>
  356. * This method breaks the <code>command</code> string into tokens and
  357. * creates a new array <code>cmdarray</code> containing the tokens in the
  358. * order that they were produced by the string tokenizer; it
  359. * then performs the call <code>exec(cmdarray, envp)</code>. The token
  360. * parsing is done by a {@link java.util.StringTokenizer} created by
  361. * the call:
  362. * <blockquote><pre>
  363. * new StringTokenizer(command)
  364. * </pre></blockquote>
  365. * with no further modification of the character categories.
  366. *
  367. * <p>
  368. * The environment variable settings are specified by <tt>envp</tt>.
  369. * If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the
  370. * environment settings of the current process.
  371. *
  372. * <p>
  373. * The working directory of the new subprocess is specified by <tt>dir</tt>.
  374. * If <tt>dir</tt> is <tt>null</tt>, the subprocess inherits the
  375. * current working directory of the current process.
  376. *
  377. * @param command a specified system command.
  378. * @param envp array of strings, each element of which
  379. * has environment variable settings in format
  380. * <i>name</i>=<i>value</i>.
  381. * @param dir the working directory of the subprocess, or
  382. * <tt>null</tt> if the subprocess should inherit
  383. * the working directory of the current process.
  384. * @return a <code>Process</code> object for managing the subprocess.
  385. * @exception SecurityException if a security manager exists and its
  386. * <code>checkExec</code> method doesn't allow creation of a subprocess.
  387. * @exception IOException if an I/O error occurs
  388. * @exception NullPointerException if <code>command</code> is
  389. * <code>null</code>
  390. * @exception IllegalArgumentException if <code>command</code> is empty
  391. * @see java.lang.Runtime#exec(java.lang.String[], java.lang.String[], File)
  392. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  393. * @since 1.3
  394. */
  395. public Process exec(String command, String envp[], File dir)
  396. throws IOException {
  397. int count = 0;
  398. String cmdarray[];
  399. StringTokenizer st;
  400. if (command.length() == 0)
  401. throw new IllegalArgumentException("Empty command");
  402. st = new StringTokenizer(command);
  403. count = st.countTokens();
  404. cmdarray = new String[count];
  405. st = new StringTokenizer(command);
  406. count = 0;
  407. while (st.hasMoreTokens()) {
  408. cmdarray[count++] = st.nextToken();
  409. }
  410. return exec(cmdarray, envp, dir);
  411. }
  412. /**
  413. * Executes the specified command and arguments in a separate process.
  414. * <p>
  415. * The command specified by the tokens in <code>cmdarray</code> is
  416. * executed as a command in a separate process. This has exactly the
  417. * same effect as <code>exec(cmdarray, null)</code>.
  418. * <p>
  419. * If there is a security manager, its <code>checkExec</code>
  420. * method is called with the first component of the array
  421. * <code>cmdarray</code> as its argument. This may result in a security
  422. * exception.
  423. *
  424. * @param cmdarray array containing the command to call and
  425. * its arguments.
  426. * @return a <code>Process</code> object for managing the subprocess.
  427. * @exception SecurityException if a security manager exists and its
  428. * <code>checkExec</code> method doesn't allow creation of a subprocess.
  429. * @exception IOException if an I/O error occurs
  430. * @exception NullPointerException if <code>cmdarray</code> is
  431. * <code>null</code>
  432. * @exception IndexOutOfBoundsException if <code>cmdarray</code> is an
  433. * empty array (has length <code>0</code>).
  434. * @see java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  435. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  436. */
  437. public Process exec(String cmdarray[]) throws IOException {
  438. return exec(cmdarray, null);
  439. }
  440. /**
  441. * Executes the specified command and arguments in a separate process
  442. * with the specified environment.
  443. * <p>
  444. * Given an array of strings <code>cmdarray</code>, representing the
  445. * tokens of a command line, and an array of strings <code>envp</code>,
  446. * representing "environment" variable settings, this method creates
  447. * a new process in which to execute the specified command.
  448. *
  449. * <p>
  450. * If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the
  451. * environment settings of the current process.
  452. *
  453. * @param cmdarray array containing the command to call and
  454. * its arguments.
  455. * @param envp array of strings, each element of which
  456. * has environment variable settings in format
  457. * <i>name</i>=<i>value</i>.
  458. * @return a <code>Process</code> object for managing the subprocess.
  459. * @exception SecurityException if a security manager exists and its
  460. * <code>checkExec</code> method doesn't allow creation of a subprocess.
  461. * @exception IOException if an I/O error occurs
  462. * @exception NullPointerException if <code>cmdarray</code> is
  463. * <code>null</code>
  464. * @exception IndexOutOfBoundsException if <code>cmdarray</code> is an
  465. * empty array (has length <code>0</code>).
  466. * @see java.lang.Process
  467. * @see java.lang.SecurityException
  468. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  469. */
  470. public Process exec(String cmdarray[], String envp[]) throws IOException {
  471. return exec(cmdarray, envp, null);
  472. }
  473. /**
  474. * Executes the specified command and arguments in a separate process with
  475. * the specified environment and working directory.
  476. * <p>
  477. * If there is a security manager, its <code>checkExec</code>
  478. * method is called with the first component of the array
  479. * <code>cmdarray</code> as its argument. This may result in a security
  480. * exception.
  481. * <p>
  482. * Given an array of strings <code>cmdarray</code>, representing the
  483. * tokens of a command line, and an array of strings <code>envp</code>,
  484. * representing "environment" variable settings, this method creates
  485. * a new process in which to execute the specified command.
  486. *
  487. * <p>
  488. * If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the
  489. * environment settings of the current process.
  490. *
  491. * <p>
  492. * The working directory of the new subprocess is specified by <tt>dir</tt>.
  493. * If <tt>dir</tt> is <tt>null</tt>, the subprocess inherits the
  494. * current working directory of the current process.
  495. *
  496. *
  497. * @param cmdarray array containing the command to call and
  498. * its arguments.
  499. * @param envp array of strings, each element of which
  500. * has environment variable settings in format
  501. * <i>name</i>=<i>value</i>.
  502. * @param dir the working directory of the subprocess, or
  503. * <tt>null</tt> if the subprocess should inherit
  504. * the working directory of the current process.
  505. * @return a <code>Process</code> object for managing the subprocess.
  506. * @exception SecurityException if a security manager exists and its
  507. * <code>checkExec</code> method doesn't allow creation of a
  508. * subprocess.
  509. * @exception NullPointerException if <code>cmdarray</code> is
  510. * <code>null</code>
  511. * @exception IndexOutOfBoundsException if <code>cmdarray</code> is an
  512. * empty array (has length <code>0</code>).
  513. * @exception IOException if an I/O error occurs.
  514. * @see java.lang.Process
  515. * @see java.lang.SecurityException
  516. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  517. * @since 1.3
  518. */
  519. public Process exec(String cmdarray[], String envp[], File dir)
  520. throws IOException {
  521. cmdarray = (String[])cmdarray.clone();
  522. envp = (envp != null ? (String[])envp.clone() : null);
  523. if (cmdarray.length == 0) {
  524. throw new IndexOutOfBoundsException();
  525. }
  526. for (int i = 0; i < cmdarray.length; i++) {
  527. if (cmdarray[i] == null) {
  528. throw new NullPointerException();
  529. }
  530. }
  531. if (envp != null) {
  532. for (int i = 0; i < envp.length; i++) {
  533. if (envp[i] == null) {
  534. throw new NullPointerException();
  535. }
  536. }
  537. }
  538. SecurityManager security = System.getSecurityManager();
  539. if (security != null) {
  540. security.checkExec(cmdarray[0]);
  541. }
  542. String path = (dir == null ? null : dir.getPath());
  543. return execInternal(cmdarray, envp, path);
  544. }
  545. /**
  546. * Returns the number of processors available to the Java virtual machine.
  547. *
  548. * <p> This value may change during a particular invocation of the virtual
  549. * machine. Applications that are sensitive to the number of available
  550. * processors should therefore occasionally poll this property and adjust
  551. * their resource usage appropriately. </p>
  552. *
  553. * @return the maximum number of processors available to the virtual
  554. * machine; never smaller than one
  555. */
  556. public native int availableProcessors();
  557. /**
  558. * Returns the amount of free memory in the Java Virtual Machine.
  559. * Calling the
  560. * <code>gc</code> method may result in increasing the value returned
  561. * by <code>freeMemory.</code>
  562. *
  563. * @return an approximation to the total amount of memory currently
  564. * available for future allocated objects, measured in bytes.
  565. */
  566. public native long freeMemory();
  567. /**
  568. * Returns the total amount of memory in the Java virtual machine.
  569. * The value returned by this method may vary over time, depending on
  570. * the host environment.
  571. * <p>
  572. * Note that the amount of memory required to hold an object of any
  573. * given type may be implementation-dependent.
  574. *
  575. * @return the total amount of memory currently available for current
  576. * and future objects, measured in bytes.
  577. */
  578. public native long totalMemory();
  579. /**
  580. * Returns the maximum amount of memory that the Java virtual machine will
  581. * attempt to use. If there is no inherent limit then the value {@link
  582. * java.lang.Long#MAX_VALUE} will be returned. </p>
  583. *
  584. * @return the maximum amount of memory that the virtual machine will
  585. * attempt to use, measured in bytes
  586. */
  587. public native long maxMemory();
  588. /**
  589. * Runs the garbage collector.
  590. * Calling this method suggests that the Java virtual machine expend
  591. * effort toward recycling unused objects in order to make the memory
  592. * they currently occupy available for quick reuse. When control
  593. * returns from the method call, the virtual machine has made
  594. * its best effort to recycle all discarded objects.
  595. * <p>
  596. * The name <code>gc</code> stands for "garbage
  597. * collector". The virtual machine performs this recycling
  598. * process automatically as needed, in a separate thread, even if the
  599. * <code>gc</code> method is not invoked explicitly.
  600. * <p>
  601. * The method {@link System#gc()} is the conventional and convenient
  602. * means of invoking this method.
  603. */
  604. public native void gc();
  605. /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */
  606. private static native void runFinalization0();
  607. /**
  608. * Runs the finalization methods of any objects pending finalization.
  609. * Calling this method suggests that the Java virtual machine expend
  610. * effort toward running the <code>finalize</code> methods of objects
  611. * that have been found to be discarded but whose <code>finalize</code>
  612. * methods have not yet been run. When control returns from the
  613. * method call, the virtual machine has made a best effort to
  614. * complete all outstanding finalizations.
  615. * <p>
  616. * The virtual machine performs the finalization process
  617. * automatically as needed, in a separate thread, if the
  618. * <code>runFinalization</code> method is not invoked explicitly.
  619. * <p>
  620. * The method {@link System#runFinalization()} is the conventional
  621. * and convenient means of invoking this method.
  622. *
  623. * @see java.lang.Object#finalize()
  624. */
  625. public void runFinalization() {
  626. runFinalization0();
  627. }
  628. /**
  629. * Enables/Disables tracing of instructions.
  630. * If the <code>boolean</code> argument is <code>true</code>, this
  631. * method suggests that the Java virtual machine emit debugging
  632. * information for each instruction in the virtual machine as it
  633. * is executed. The format of this information, and the file or other
  634. * output stream to which it is emitted, depends on the host environment.
  635. * The virtual machine may ignore this request if it does not support
  636. * this feature. The destination of the trace output is system
  637. * dependent.
  638. * <p>
  639. * If the <code>boolean</code> argument is <code>false</code>, this
  640. * method causes the virtual machine to stop performing the
  641. * detailed instruction trace it is performing.
  642. *
  643. * @param on <code>true</code> to enable instruction tracing;
  644. * <code>false</code> to disable this feature.
  645. */
  646. public native void traceInstructions(boolean on);
  647. /**
  648. * Enables/Disables tracing of method calls.
  649. * If the <code>boolean</code> argument is <code>true</code>, this
  650. * method suggests that the Java virtual machine emit debugging
  651. * information for each method in the virtual machine as it is
  652. * called. The format of this information, and the file or other output
  653. * stream to which it is emitted, depends on the host environment. The
  654. * virtual machine may ignore this request if it does not support
  655. * this feature.
  656. * <p>
  657. * Calling this method with argument false suggests that the
  658. * virtual machine cease emitting per-call debugging information.
  659. *
  660. * @param on <code>true</code> to enable instruction tracing;
  661. * <code>false</code> to disable this feature.
  662. */
  663. public native void traceMethodCalls(boolean on);
  664. /**
  665. * Loads the specified filename as a dynamic library. The filename
  666. * argument must be a complete path name.
  667. * From <code>java_g</code> it will automagically insert "_g" before the
  668. * ".so" (for example
  669. * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>).
  670. * <p>
  671. * First, if there is a security manager, its <code>checkLink</code>
  672. * method is called with the <code>filename</code> as its argument.
  673. * This may result in a security exception.
  674. * <p>
  675. * This is similar to the method {@link #loadLibrary(String)}, but it
  676. * accepts a general file name as an argument rather than just a library
  677. * name, allowing any file of native code to be loaded.
  678. * <p>
  679. * The method {@link System#load(String)} is the conventional and
  680. * convenient means of invoking this method.
  681. *
  682. * @param filename the file to load.
  683. * @exception SecurityException if a security manager exists and its
  684. * <code>checkLink</code> method doesn't allow
  685. * loading of the specified dynamic library
  686. * @exception UnsatisfiedLinkError if the file does not exist.
  687. * @see java.lang.Runtime#getRuntime()
  688. * @see java.lang.SecurityException
  689. * @see java.lang.SecurityManager#checkLink(java.lang.String)
  690. */
  691. public void load(String filename) {
  692. load0(System.getCallerClass(), filename);
  693. }
  694. synchronized void load0(Class fromClass, String filename) {
  695. SecurityManager security = System.getSecurityManager();
  696. if (security != null) {
  697. security.checkLink(filename);
  698. }
  699. if (!(new File(filename).isAbsolute())) {
  700. throw new UnsatisfiedLinkError(
  701. "Expecting an absolute path of the library: " + filename);
  702. }
  703. ClassLoader.loadLibrary(fromClass, filename, true);
  704. }
  705. /**
  706. * Loads the dynamic library with the specified library name.
  707. * A file containing native code is loaded from the local file system
  708. * from a place where library files are conventionally obtained. The
  709. * details of this process are implementation-dependent. The
  710. * mapping from a library name to a specific filename is done in a
  711. * system-specific manner.
  712. * <p>
  713. * First, if there is a security manager, its <code>checkLink</code>
  714. * method is called with the <code>libname</code> as its argument.
  715. * This may result in a security exception.
  716. * <p>
  717. * The method {@link System#loadLibrary(String)} is the conventional
  718. * and convenient means of invoking this method. If native
  719. * methods are to be used in the implementation of a class, a standard
  720. * strategy is to put the native code in a library file (call it
  721. * <code>LibFile</code>) and then to put a static initializer:
  722. * <blockquote><pre>
  723. * static { System.loadLibrary("LibFile"); }
  724. * </pre></blockquote>
  725. * within the class declaration. When the class is loaded and
  726. * initialized, the necessary native code implementation for the native
  727. * methods will then be loaded as well.
  728. * <p>
  729. * If this method is called more than once with the same library
  730. * name, the second and subsequent calls are ignored.
  731. *
  732. * @param libname the name of the library.
  733. * @exception SecurityException if a security manager exists and its
  734. * <code>checkLink</code> method doesn't allow
  735. * loading of the specified dynamic library
  736. * @exception UnsatisfiedLinkError if the library does not exist.
  737. * @see java.lang.SecurityException
  738. * @see java.lang.SecurityManager#checkLink(java.lang.String)
  739. */
  740. public void loadLibrary(String libname) {
  741. loadLibrary0(System.getCallerClass(), libname);
  742. }
  743. synchronized void loadLibrary0(Class fromClass, String libname) {
  744. SecurityManager security = System.getSecurityManager();
  745. if (security != null) {
  746. security.checkLink(libname);
  747. }
  748. if (libname.indexOf((int)File.separatorChar) != -1) {
  749. throw new UnsatisfiedLinkError(
  750. "Directory separator should not appear in library name: " + libname);
  751. }
  752. ClassLoader.loadLibrary(fromClass, libname, false);
  753. }
  754. /**
  755. * Creates a localized version of an input stream. This method takes
  756. * an <code>InputStream</code> and returns an <code>InputStream</code>
  757. * equivalent to the argument in all respects except that it is
  758. * localized: as characters in the local character set are read from
  759. * the stream, they are automatically converted from the local
  760. * character set to Unicode.
  761. * <p>
  762. * If the argument is already a localized stream, it may be returned
  763. * as the result.
  764. *
  765. * @param in InputStream to localize
  766. * @return a localized input stream
  767. * @see java.io.InputStream
  768. * @see java.io.BufferedReader#BufferedReader(java.io.Reader)
  769. * @see java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
  770. * @deprecated As of JDK 1.1, the preferred way to translate a byte
  771. * stream in the local encoding into a character stream in Unicode is via
  772. * the <code>InputStreamReader</code> and <code>BufferedReader</code>
  773. * classes.
  774. */
  775. public InputStream getLocalizedInputStream(InputStream in) {
  776. return in;
  777. }
  778. /**
  779. * Creates a localized version of an output stream. This method
  780. * takes an <code>OutputStream</code> and returns an
  781. * <code>OutputStream</code> equivalent to the argument in all respects
  782. * except that it is localized: as Unicode characters are written to
  783. * the stream, they are automatically converted to the local
  784. * character set.
  785. * <p>
  786. * If the argument is already a localized stream, it may be returned
  787. * as the result.
  788. *
  789. * @deprecated As of JDK 1.1, the preferred way to translate a
  790. * Unicode character stream into a byte stream in the local encoding is via
  791. * the <code>OutputStreamWriter</code>, <code>BufferedWriter</code>, and
  792. * <code>PrintWriter</code> classes.
  793. *
  794. * @param out OutputStream to localize
  795. * @return a localized output stream
  796. * @see java.io.OutputStream
  797. * @see java.io.BufferedWriter#BufferedWriter(java.io.Writer)
  798. * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  799. * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
  800. */
  801. public OutputStream getLocalizedOutputStream(OutputStream out) {
  802. return out;
  803. }
  804. }