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