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