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