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