1. /*
  2. * @(#)System.java 1.102 01/11/29
  3. *
  4. * Copyright 2002 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.Properties;
  10. import java.util.PropertyPermission;
  11. import java.util.StringTokenizer;
  12. import java.security.AccessController;
  13. import java.security.PrivilegedAction;
  14. import java.security.AllPermission;
  15. import sun.net.InetAddressCachePolicy;
  16. /**
  17. * The <code>System</code> class contains several useful class fields
  18. * and methods. It cannot be instantiated.
  19. * <p>
  20. * Among the facilities provided by the <code>System</code> class
  21. * are standard input, standard output, and error output streams;
  22. * access to externally defined "properties"; a means of
  23. * loading files and libraries; and a utility method for quickly
  24. * copying a portion of an array.
  25. *
  26. * @author Arthur van Hoff
  27. * @version 1.102, 11/29/01
  28. * @since JDK1.0
  29. */
  30. public final class System {
  31. /* First thing---register the natives */
  32. private static native void registerNatives();
  33. static {
  34. registerNatives();
  35. }
  36. /** Don't let anyone instantiate this class */
  37. private System() {
  38. }
  39. /**
  40. * The "standard" input stream. This stream is already
  41. * open and ready to supply input data. Typically this stream
  42. * corresponds to keyboard input or another input source specified by
  43. * the host environment or user.
  44. */
  45. public final static InputStream in = nullInputStream();
  46. /**
  47. * The "standard" output stream. This stream is already
  48. * open and ready to accept output data. Typically this stream
  49. * corresponds to display output or another output destination
  50. * specified by the host environment or user.
  51. * <p>
  52. * For simple stand-alone Java applications, a typical way to write
  53. * a line of output data is:
  54. * <blockquote><pre>
  55. * System.out.println(data)
  56. * </pre></blockquote>
  57. * <p>
  58. * See the <code>println</code> methods in class <code>PrintStream</code>.
  59. *
  60. * @see java.io.PrintStream#println()
  61. * @see java.io.PrintStream#println(boolean)
  62. * @see java.io.PrintStream#println(char)
  63. * @see java.io.PrintStream#println(char[])
  64. * @see java.io.PrintStream#println(double)
  65. * @see java.io.PrintStream#println(float)
  66. * @see java.io.PrintStream#println(int)
  67. * @see java.io.PrintStream#println(long)
  68. * @see java.io.PrintStream#println(java.lang.Object)
  69. * @see java.io.PrintStream#println(java.lang.String)
  70. */
  71. public final static PrintStream out = nullPrintStream();
  72. /**
  73. * The "standard" error output stream. This stream is already
  74. * open and ready to accept output data.
  75. * <p>
  76. * Typically this stream corresponds to display output or another
  77. * output destination specified by the host environment or user. By
  78. * convention, this output stream is used to display error messages
  79. * or other information that should come to the immediate attention
  80. * of a user even if the principal output stream, the value of the
  81. * variable <code>out</code>, has been redirected to a file or other
  82. * destination that is typically not continuously monitored.
  83. */
  84. public final static PrintStream err = nullPrintStream();
  85. /* The security manager for the system.
  86. */
  87. private static SecurityManager security = null;
  88. /**
  89. * Reassigns the "standard" input stream.
  90. *
  91. * <p>First, if there is a security manager, its <code>checkPermission</code>
  92. * method is called with a <code>RuntimePermission("setIO")</code> permission
  93. * to see if it's ok to reassign the "standard" input stream.
  94. * <p>
  95. *
  96. * @param in the new standard input stream.
  97. *
  98. * @throws SecurityException
  99. * if a security manager exists and its
  100. * <code>checkPermission</code> method doesn't allow
  101. * reassigning of the standard input stream.
  102. *
  103. * @see SecurityManager#checkPermission
  104. * @see java.lang.RuntimePermission
  105. *
  106. * @since JDK1.1
  107. */
  108. public static void setIn(InputStream in) {
  109. checkIO();
  110. setIn0(in);
  111. }
  112. /**
  113. * Reassigns the "standard" output stream.
  114. *
  115. * <p>First, if there is a security manager, its <code>checkPermission</code>
  116. * method is called with a <code>RuntimePermission("setIO")</code> permission
  117. * to see if it's ok to reassign the "standard" output stream.
  118. *
  119. * @param out the new standard output stream.
  120. *
  121. * @throws SecurityException
  122. * if a security manager exists and its
  123. * <code>checkPermission</code> method doesn't allow
  124. * reassigning of the standard output stream.
  125. *
  126. * @see SecurityManager#checkPermission
  127. * @see java.lang.RuntimePermission
  128. *
  129. * @since JDK1.1
  130. */
  131. public static void setOut(PrintStream out) {
  132. checkIO();
  133. setOut0(out);
  134. }
  135. /**
  136. * Reassigns the "standard" error output stream.
  137. *
  138. * <p>First, if there is a security manager, its <code>checkPermission</code>
  139. * method is called with a <code>RuntimePermission("setIO")</code> permission
  140. * to see if it's ok to reassign the "standard" error output stream.
  141. *
  142. * @param out the new standard error output stream.
  143. *
  144. * @throws SecurityException
  145. * if a security manager exists and its
  146. * <code>checkPermission</code> method doesn't allow
  147. * reassigning of the standard error output stream.
  148. *
  149. * @see SecurityManager#checkPermission
  150. * @see java.lang.RuntimePermission
  151. *
  152. * @since JDK1.1
  153. */
  154. public static void setErr(PrintStream err) {
  155. checkIO();
  156. setErr0(err);
  157. }
  158. private static void checkIO() {
  159. if (security != null)
  160. security.checkPermission(new RuntimePermission("setIO"));
  161. }
  162. private static native void setIn0(InputStream in);
  163. private static native void setOut0(PrintStream out);
  164. private static native void setErr0(PrintStream err);
  165. /**
  166. * Sets the System security.
  167. *
  168. * <p> If there is a security manager already installed, this method first
  169. * calls the security manager's <code>checkPermission</code> method
  170. * with a <code>RuntimePermission("setSecurityManager")</code>
  171. * permission to ensure it's ok to replace the existing
  172. * security manager.
  173. * This may result in throwing a <code>SecurityException</code>.
  174. *
  175. * <p> Otherwise, the argument is established as the current
  176. * security manager. If the argument is <code>null</code> and no
  177. * security manager has been established, then no action is taken and
  178. * the method simply returns.
  179. *
  180. * @param s the security manager.
  181. * @exception SecurityException if the security manager has already
  182. * been set and its <code>checkPermission</code> method
  183. * doesn't allow it to be replaced.
  184. * @see SecurityManager#checkPermission
  185. * @see java.lang.RuntimePermission
  186. */
  187. public static
  188. void setSecurityManager(final SecurityManager s) {
  189. try {
  190. s.checkPackageAccess("java.lang");
  191. } catch (Exception e) {
  192. // no-op
  193. }
  194. setSecurityManager0(s);
  195. }
  196. private static synchronized
  197. void setSecurityManager0(final SecurityManager s) {
  198. if (security != null) {
  199. // ask the currently installed security manager if we
  200. // can replace it.
  201. security.checkPermission(new RuntimePermission
  202. ("setSecurityManager"));
  203. }
  204. if ((s != null) && (s.getClass().getClassLoader() != null)) {
  205. // New security manager class is not on bootstrap classpath.
  206. // Cause policy to get initialized before we install the new
  207. // security manager, in order to prevent infinite loops when
  208. // trying to initialize the policy (which usually involves
  209. // accessing some security and/or system properties, which in turn
  210. // calls the installed security manager's checkPermission method
  211. // which will loop infinitely if there is a non-system class
  212. // (in this case: the new security manager class) on the stack).
  213. AccessController.doPrivileged(new PrivilegedAction() {
  214. public Object run() {
  215. s.getClass().getProtectionDomain().implies
  216. (new AllPermission());
  217. return null;
  218. }
  219. });
  220. }
  221. security = s;
  222. InetAddressCachePolicy.setIfNotSet(InetAddressCachePolicy.FOREVER);
  223. }
  224. /**
  225. * Gets the system security interface.
  226. *
  227. * @return if a security manager has already been established for the
  228. * current application, then that security manager is returned;
  229. * otherwise, <code>null</code> is returned.
  230. */
  231. public static SecurityManager getSecurityManager() {
  232. return security;
  233. }
  234. /**
  235. * Returns the current time in milliseconds.
  236. * <p>
  237. * See the description of the class <code>Date</code> for a discussion
  238. * of slight discrepancies that may arise between "computer
  239. * time" and coordinated universal time (UTC).
  240. *
  241. * @return the difference, measured in milliseconds, between the current
  242. * time and midnight, January 1, 1970 UTC.
  243. * @see java.util.Date
  244. */
  245. public static native long currentTimeMillis();
  246. /**
  247. * Copies an array from the specified source array, beginning at the
  248. * specified position, to the specified position of the destination array.
  249. * A subsequence of array components are copied from the source
  250. * array referenced by <code>src</code> to the destination array
  251. * referenced by <code>dst</code>. The number of components copied is
  252. * equal to the <code>length</code> argument. The components at
  253. * positions <code>srcOffset</code> through
  254. * <code>srcOffset+length-1</code> in the source array are copied into
  255. * positions <code>dstOffset</code> through
  256. * <code>dstOffset+length-1</code>, respectively, of the destination
  257. * array.
  258. * <p>
  259. * If the <code>src</code> and <code>dst</code> arguments refer to the
  260. * same array object, then the copying is performed as if the
  261. * components at positions <code>srcOffset</code> through
  262. * <code>srcOffset+length-1</code> were first copied to a temporary
  263. * array with <code>length</code> components and then the contents of
  264. * the temporary array were copied into positions
  265. * <code>dstOffset</code> through <code>dstOffset+length-1</code> of the
  266. * destination array.
  267. * <p>
  268. * If <code>dst</code> is <code>null</code>, then a
  269. * <code>NullPointerException</code> is thrown.
  270. * <p>
  271. * If <code>src</code> is <code>null</code>, then a
  272. * <code>NullPointerException</code> is thrown and the destination
  273. * array is not modified.
  274. * <p>
  275. * Otherwise, if any of the following is true, an
  276. * <code>ArrayStoreException</code> is thrown and the destination is
  277. * not modified:
  278. * <ul>
  279. * <li>The <code>src</code> argument refers to an object that is not an
  280. * array.
  281. * <li>The <code>dst</code> argument refers to an object that is not an
  282. * array.
  283. * <li>The <code>src</code> argument and <code>dst</code> argument refer to
  284. * arrays whose component types are different primitive types.
  285. * <li>The <code>src</code> argument refers to an array with a primitive
  286. * component type and the <code>dst</code> argument refers to an array
  287. * with a reference component type.
  288. * <li>The <code>src</code> argument refers to an array with a reference
  289. * component type and the <code>dst</code> argument refers to an array
  290. * with a primitive component type.
  291. * </ul>
  292. * <p>
  293. * Otherwise, if any of the following is true, an
  294. * <code>IndexOutOfBoundsException</code> is
  295. * thrown and the destination is not modified:
  296. * <ul>
  297. * <li>The <code>srcOffset</code> argument is negative.
  298. * <li>The <code>dstOffset</code> argument is negative.
  299. * <li>The <code>length</code> argument is negative.
  300. * <li><code>srcOffset+length</code> is greater than
  301. * <code>src.length</code>, the length of the source array.
  302. * <li><code>dstOffset+length</code> is greater than
  303. * <code>dst.length</code>, the length of the destination array.
  304. * </ul>
  305. * <p>
  306. * Otherwise, if any actual component of the source array from
  307. * position <code>srcOffset</code> through
  308. * <code>srcOffset+length-1</code> cannot be converted to the component
  309. * type of the destination array by assignment conversion, an
  310. * <code>ArrayStoreException</code> is thrown. In this case, let
  311. * <b><i>k</i></b> be the smallest nonnegative integer less than
  312. * length such that <code>src[srcOffset+</code><i>k</i><code>]</code>
  313. * cannot be converted to the component type of the destination
  314. * array; when the exception is thrown, source array components from
  315. * positions <code>srcOffset</code> through
  316. * <code>srcOffset+</code><i>k</i><code>-1</code>
  317. * will already have been copied to destination array positions
  318. * <code>dstOffset</code> through
  319. * <code>dstOffset+</code><i>k</I><code>-1</code> and no other
  320. * positions of the destination array will have been modified.
  321. * (Because of the restrictions already itemized, this
  322. * paragraph effectively applies only to the situation where both
  323. * arrays have component types that are reference types.)
  324. *
  325. * @param src: the source array.
  326. * @param srcpos start position in the source array.
  327. * @param dest the destination array.
  328. * @param destpos start position in the destination data.
  329. * @param length the number of array elements to be copied.
  330. * @exception IndexOutOfBoundsException if copying would cause
  331. * access of data outside array bounds.
  332. * @exception ArrayStoreException if an element in the <code>src</code>
  333. * array could not be stored into the <code>dest</code> array
  334. * because of a type mismatch.
  335. * @exception NullPointerException if either <code>src</code> or
  336. * <code>dst</code> is <code>null</code>.
  337. */
  338. public static native void arraycopy(Object src, int src_position,
  339. Object dst, int dst_position,
  340. int length);
  341. /**
  342. * Returns the same hashcode for the given object as
  343. * would be returned by the default method hashCode(),
  344. * whether or not the given object's class overrides
  345. * hashCode().
  346. * The hashcode for the null reference is zero.
  347. *
  348. * @since JDK1.1
  349. */
  350. public static native int identityHashCode(Object x);
  351. /**
  352. * System properties. The following properties are guaranteed to be defined:
  353. * <dl>
  354. * <dt>java.version <dd>Java version number
  355. * <dt>java.vendor <dd>Java vendor specific string
  356. * <dt>java.vendor.url <dd>Java vendor URL
  357. * <dt>java.home <dd>Java installation directory
  358. * <dt>java.class.version <dd>Java class version number
  359. * <dt>java.class.path <dd>Java classpath
  360. * <dt>os.name <dd>Operating System Name
  361. * <dt>os.arch <dd>Operating System Architecture
  362. * <dt>os.version <dd>Operating System Version
  363. * <dt>file.separator <dd>File separator ("/" on Unix)
  364. * <dt>path.separator <dd>Path separator (":" on Unix)
  365. * <dt>line.separator <dd>Line separator ("\n" on Unix)
  366. * <dt>user.name <dd>User account name
  367. * <dt>user.home <dd>User home directory
  368. * <dt>user.dir <dd>User's current working directory
  369. * </dl>
  370. */
  371. private static Properties props;
  372. private static native Properties initProperties(Properties props);
  373. /**
  374. * Determines the current system properties.
  375. * <p>
  376. * First, if there is a security manager, its
  377. * <code>checkPropertiesAccess</code> method is called with no
  378. * arguments. This may result in a security exception.
  379. * <p>
  380. * The current set of system properties for use by the
  381. * {@link #getProperty(String)} method is returned as a
  382. * <code>Properties</code> object. If there is no current set of
  383. * system properties, a set of system properties is first created and
  384. * initialized. This set of system properties always includes values
  385. * for the following keys:
  386. * <table>
  387. * <tr><th>Key</th>
  388. * <th>Description of Associated Value</th></tr>
  389. * <tr><td><code>java.version</code></td>
  390. * <td>Java Runtime Environment version</td></tr>
  391. * <tr><td><code>java.vendor</code></td>
  392. * <td>Java Runtime Environment vendor</td></tr
  393. * <tr><td><code>java.vendor.url</code></td>
  394. * <td>Java vendor URL</td></tr>
  395. * <tr><td><code>java.home</code></td>
  396. * <td>Java installation directory</td></tr>
  397. * <tr><td><code>java.vm.specification.version</code></td>
  398. * <td>Java Virtual Machine specification version</td></tr>
  399. * <tr><td><code>java.vm.specification.vendor</code></td>
  400. * <td>Java Virtual Machine specification vendor</td></tr>
  401. * <tr><td><code>java.vm.specification.name</code></td>
  402. * <td>Java Virtual Machine specification name</td></tr>
  403. * <tr><td><code>java.vm.version</code></td>
  404. * <td>Java Virtual Machine implementation version</td></tr>
  405. * <tr><td><code>java.vm.vendor</code></td>
  406. * <td>Java Virtual Machine implementation vendor</td></tr>
  407. * <tr><td><code>java.vm.name</code></td>
  408. * <td>Java Virtual Machine implementation name</td></tr>
  409. * <tr><td><code>java.specification.version</code></td>
  410. * <td>Java Runtime Environment specification version</td></tr>
  411. * <tr><td><code>java.specification.vendor</code></td>
  412. * <td>Java Runtime Environment specification vendor</td></tr>
  413. * <tr><td><code>java.specification.name</code></td>
  414. * <td>Java Runtime Environment specification name</td></tr>
  415. * <tr><td><code>java.class.version</code></td>
  416. * <td>Java class format version number</td></tr>
  417. * <tr><td><code>java.class.path</code></td>
  418. * <td>Java class path</td></tr>
  419. * <tr><td><code>os.name</code></td>
  420. * <td>Operating system name</td></tr>
  421. * <tr><td><code>os.arch</code></td>
  422. * <td>Operating system architecture</td></tr>
  423. * <tr><td><code>os.version</code></td>
  424. * <td>Operating system version</td></tr>
  425. * <tr><td><code>file.separator</code></td>
  426. * <td>File separator ("/" on UNIX)</td></tr>
  427. * <tr><td><code>path.separator</code></td>
  428. * <td>Path separator (":" on UNIX)</td></tr>
  429. * <tr><td><code>line.separator</code></td>
  430. * <td>Line separator ("\n" on UNIX)</td></tr>
  431. * <tr><td><code>user.name</code></td>
  432. * <td>User's account name</td></tr>
  433. * <tr><td><code>user.home</code></td>
  434. * <td>User's home directory</td></tr>
  435. * <tr><td><code>user.dir</code></td>
  436. * <td>User's current working directory</td></tr>
  437. * </table>
  438. * <p>
  439. * Note that even if the security manager does not permit the
  440. * <code>getProperties</code> operation, it may choose to permit the
  441. * {@link #getProperty(String)} operation.
  442. *
  443. * @exception SecurityException if a security manager exists and its
  444. * <code>checkPropertiesAccess</code> method doesn't allow access
  445. * to the system properties.
  446. * @see java.lang.SecurityException
  447. * @see java.lang.SecurityManager#checkPropertiesAccess()
  448. * @see java.util.Properties
  449. */
  450. public static Properties getProperties() {
  451. if (security != null) {
  452. security.checkPropertiesAccess();
  453. }
  454. return props;
  455. }
  456. /**
  457. * Sets the system properties to the <code>Properties</code>
  458. * argument.
  459. * <p>
  460. * First, if there is a security manager, its
  461. * <code>checkPropertiesAccess</code> method is called with no
  462. * arguments. This may result in a security exception.
  463. * <p>
  464. * The argument becomes the current set of system properties for use
  465. * by the {@link #getProperty(String)} method. If the argument is
  466. * <code>null</code>, then the current set of system properties is
  467. * forgotten.
  468. *
  469. * @param props the new system properties.
  470. * @exception SecurityException if a security manager exists and its
  471. * <code>checkPropertiesAccess</code> method doesn't allow access
  472. * to the system properties.
  473. * @see java.lang.Properties
  474. * @see java.lang.SecurityException
  475. * @see java.lang.SecurityManager#checkPropertiesAccess()
  476. */
  477. public static void setProperties(Properties props) {
  478. if (security != null) {
  479. security.checkPropertiesAccess();
  480. }
  481. System.props = props;
  482. }
  483. /**
  484. * Gets the system property indicated by the specified key.
  485. * <p>
  486. * First, if there is a security manager, its
  487. * <code>checkPropertyAccess</code> method is called with the key as
  488. * its argument. This may result in a SecurityException.
  489. * <p>
  490. * If there is no current set of system properties, a set of system
  491. * properties is first created and initialized in the same manner as
  492. * for the <code>getProperties</code> method.
  493. *
  494. * @param key the name of the system property.
  495. * @return the string value of the system property,
  496. * or <code>null</code> if there is no property with that key.
  497. * @exception SecurityException if a security manager exists and its
  498. * <code>checkPropertyAccess</code> method doesn't allow access
  499. * to the specified system property.
  500. * @see java.lang.SecurityException
  501. * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
  502. * @see java.lang.System#getProperties()
  503. */
  504. public static String getProperty(String key) {
  505. if (security != null) {
  506. security.checkPropertyAccess(key);
  507. }
  508. return props.getProperty(key);
  509. }
  510. /**
  511. * Gets the system property indicated by the specified key.
  512. * <p>
  513. * First, if there is a security manager, its
  514. * <code>checkPropertyAccess</code> method is called with the
  515. * <code>key</code> as its argument.
  516. * <p>
  517. * If there is no current set of system properties, a set of system
  518. * properties is first created and initialized in the same manner as
  519. * for the <code>getProperties</code> method.
  520. *
  521. * @param key the name of the system property.
  522. * @param def a default value.
  523. * @return the string value of the system property,
  524. * or the default value if there is no property with that key.
  525. * @exception SecurityException if a security manager exists and its
  526. * <code>checkPropertyAccess</code> method doesn't allow access
  527. * to the specified system property.
  528. * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
  529. * @see java.lang.System#getProperties()
  530. */
  531. public static String getProperty(String key, String def) {
  532. if (security != null) {
  533. security.checkPropertyAccess(key);
  534. }
  535. return props.getProperty(key, def);
  536. }
  537. /**
  538. * Sets the system property indicated by the specified key.
  539. * <p>
  540. * First, if a security manager exists, its
  541. * <code>SecurityManager.checkPermission</code> method
  542. * is called with a <code>PropertyPermission(key, "write")</code>
  543. * permission. This may result in a SecurityException being thrown.
  544. * If no exception is thrown, the specified property is set to the given
  545. * value.
  546. * <p>
  547. *
  548. * @param key the name of the system property.
  549. * @param value the value of the system property.
  550. * @return the previous value of the system property,
  551. * or <code>null</code> if it did not have one.
  552. * @exception SecurityException if a security manager exists and its
  553. * <code>checkPermission</code> method doesn't allow
  554. * setting of the specified property.
  555. * @see java.lang.System#getProperty(java.lang.String)
  556. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  557. * @see java.util.PropertyPermission
  558. * @see SecurityManager#checkPermission
  559. * @since JDK1.2
  560. */
  561. public static String setProperty(String key, String value) {
  562. if (security != null)
  563. security.checkPermission(new PropertyPermission(key, "write"));
  564. return (String) props.put(key, value);
  565. }
  566. /**
  567. * Gets an environment variable. An environment variable is a
  568. * system-dependent external variable that has a string value.
  569. *
  570. * @deprecated The preferred way to extract system-dependent information
  571. * is the system properties of the
  572. * <code>java.lang.System.getProperty</code> methods and the
  573. * corresponding <code>get</code><em>TypeName</em> methods of
  574. * the <code>Boolean</code>, <code>Integer</code>, and
  575. * <code>Long</code> primitive types. For example:
  576. * <blockquote><pre>
  577. * String classPath = System.getProperty("java.class.path",".");
  578. * <br>
  579. * if (Boolean.getBoolean("myapp.exper.mode"))
  580. * enableExpertCommands();
  581. * </pre></blockquote>
  582. *
  583. * @param the name of the environment variable.
  584. * @return the value of the variable, or <code>null</code> if the variable
  585. * is not defined.
  586. * @see java.lang.Boolean#getBoolean(java.lang.String)
  587. * @see java.lang.Integer#getInteger(java.lang.String)
  588. * @see java.lang.Integer#getInteger(java.lang.String, int)
  589. * @see java.lang.Integer#getInteger(java.lang.String, java.lang.Integer)
  590. * @see java.lang.Long#getLong(java.lang.String)
  591. * @see java.lang.Long#getLong(java.lang.String, long)
  592. * @see java.lang.Long#getLong(java.lang.String, java.lang.Long)
  593. * @see java.lang.System#getProperties()
  594. * @see java.lang.System#getProperty(java.lang.String)
  595. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  596. */
  597. public static String getenv(String name) {
  598. throw new Error("getenv no longer supported, use properties and -D instead: " + name);
  599. }
  600. /**
  601. * Terminates the currently running Java Virtual Machine. The
  602. * argument serves as a status code; by convention, a nonzero status
  603. * code indicates abnormal termination.
  604. * <p>
  605. * This method calls the <code>exit</code> method in class
  606. * <code>Runtime</code>. This method never returns normally.
  607. * <p>
  608. * The call <code>System.exit(n)</code> is effectively equivalent to
  609. * the call:
  610. * <blockquote><pre>
  611. * Runtime.getRuntime().exit(n)
  612. * </pre></blockquote>
  613. *
  614. * @param status exit status.
  615. * @throws SecurityException
  616. * if a security manager exists and its <code>checkExit</code>
  617. * method doesn't allow exit with the specified status.
  618. * @see java.lang.Runtime#exit(int)
  619. */
  620. public static void exit(int status) {
  621. Runtime.getRuntime().exit(status);
  622. }
  623. /**
  624. * Runs the garbage collector.
  625. * <p>
  626. * Calling the <code>gc</code> method suggests that the Java Virtual
  627. * Machine expend effort toward recycling unused objects in order to
  628. * make the memory they currently occupy available for quick reuse.
  629. * When control returns from the method call, the Java Virtual
  630. * Machine has made a best effort to reclaim space from all discarded
  631. * objects.
  632. * <p>
  633. * The call <code>System.gc()</code> is effectively equivalent to the
  634. * call:
  635. * <blockquote><pre>
  636. * Runtime.getRuntime().gc()
  637. * </pre></blockquote>
  638. *
  639. * @see java.lang.Runtime#gc()
  640. */
  641. public static void gc() {
  642. Runtime.getRuntime().gc();
  643. }
  644. /**
  645. * Runs the finalization methods of any objects pending finalization.
  646. * <p>
  647. * Calling this method suggests that the Java Virtual Machine expend
  648. * effort toward running the <code>finalize</code> methods of objects
  649. * that have been found to be discarded but whose <code>finalize</code>
  650. * methods have not yet been run. When control returns from the
  651. * method call, the Java Virtual Machine has made a best effort to
  652. * complete all outstanding finalizations.
  653. * <p>
  654. * The call <code>System.runFinalization()</code> is effectively
  655. * equivalent to the call:
  656. * <blockquote><pre>
  657. * Runtime.getRuntime().runFinalization()
  658. * </pre></blockquote>
  659. *
  660. * @see java.lang.Runtime#runFinalization()
  661. */
  662. public static void runFinalization() {
  663. Runtime.getRuntime().runFinalization();
  664. }
  665. /**
  666. * Enable or disable finalization on exit; doing so specifies that the
  667. * finalizers of all objects that have finalizers that have not yet been
  668. * automatically invoked are to be run before the Java runtime exits.
  669. * By default, finalization on exit is disabled.
  670. *
  671. * <p>If there is a security manager,
  672. * its <code>checkExit</code> method is first called
  673. * with 0 as its argument to ensure the exit is allowed.
  674. * This could result in a SecurityException.
  675. *
  676. * @deprecated This method is inherently unsafe. It may result in
  677. * finalizers being called on live objects while other threads are
  678. * concurrently manipulating those objects, resulting in erratic
  679. * behavior or deadlock.
  680. *
  681. * @throws SecurityException
  682. * if a security manager exists and its <code>checkExit</code>
  683. * method doesn't allow the exit.
  684. *
  685. * @see java.lang.Runtime#exit(int)
  686. * @see java.lang.Runtime#gc()
  687. * @see java.lang.SecurityManager#checkExit(int)
  688. * @since JDK1.1
  689. */
  690. public static void runFinalizersOnExit(boolean value) {
  691. Runtime.getRuntime().runFinalizersOnExit(value);
  692. }
  693. /**
  694. * Loads a code file with the specified filename from the local file
  695. * system as a dynamic library. The filename
  696. * argument must be a complete pathname.
  697. * <p>
  698. * The call <code>System.load(name)</code> is effectively equivalent
  699. * to the call:
  700. * <blockquote><pre>
  701. * Runtime.getRuntime().load(name)
  702. * </pre></blockquote>
  703. *
  704. * @param filename the file to load.
  705. * @exception SecurityException if a security manager exists and its
  706. * <code>checkLink</code> method doesn't allow
  707. * loading of the specified dynamic library
  708. * @exception UnsatisfiedLinkError if the file does not exist.
  709. * @see java.lang.Runtime#load(java.lang.String)
  710. * @see java.lang.SecurityManager#checkLink(java.lang.String)
  711. */
  712. public static void load(String filename) {
  713. Runtime.getRuntime().load0(getCallerClass(), filename);
  714. }
  715. /**
  716. * Loads the system library specified by the <code>libname</code>
  717. * argument. The manner in which a library name is mapped to the
  718. * actual system library is system dependent.
  719. * <p>
  720. * The call <code>System.loadLibrary(name)</code> is effectively
  721. * equivalent to the call
  722. * <blockquote><pre>
  723. * Runtime.getRuntime().loadLibrary(name)
  724. * </pre></blockquote>
  725. *
  726. * @param libname the name of the library.
  727. * @exception SecurityException if a security manager exists and its
  728. * <code>checkLink</code> method doesn't allow
  729. * loading of the specified dynamic library
  730. * @exception UnsatisfiedLinkError if the library does not exist.
  731. * @see java.lang.Runtime#loadLibrary(java.lang.String)
  732. * @see java.lang.SecurityManager#checkLink(java.lang.String)
  733. */
  734. public static void loadLibrary(String libname) {
  735. Runtime.getRuntime().loadLibrary0(getCallerClass(), libname);
  736. }
  737. /**
  738. * Maps a library name into a platform-specific string representing
  739. * a native library.
  740. *
  741. * @param libname the name of the library.
  742. * @return a platform-dependent native library name.
  743. * @see java.lang.System#loadLibrary(java.lang.String)
  744. * @see java.lang.ClassLoader#findLibrary(java.lang.String)
  745. * @since JDK1.2
  746. */
  747. public static native String mapLibraryName(String libname);
  748. /**
  749. * The following two methods exist because in, out, and err must be
  750. * initialized to null. The compiler, however, cannot be permitted to
  751. * inline access to them, since they are later set to more sensible values
  752. * by initializeSystemClass().
  753. */
  754. private static InputStream nullInputStream() throws NullPointerException {
  755. if (currentTimeMillis() > 0)
  756. return null;
  757. throw new NullPointerException();
  758. }
  759. private static PrintStream nullPrintStream() throws NullPointerException {
  760. if (currentTimeMillis() > 0)
  761. return null;
  762. throw new NullPointerException();
  763. }
  764. /**
  765. * Initialize the system class. Called after thread initialization.
  766. */
  767. private static void initializeSystemClass() {
  768. props = new Properties();
  769. initProperties(props);
  770. FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
  771. FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
  772. FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
  773. setIn0(new BufferedInputStream(fdIn));
  774. setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
  775. setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
  776. }
  777. /* returns the class of the caller. */
  778. static native Class getCallerClass();
  779. }