ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/lang/System.java
Revision: 1.19
Committed: Sun Nov 21 01:40:36 2004 UTC (19 years, 6 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.18: +0 -0 lines
State: FILE REMOVED
Log Message:
Changes for maintenance/RFE phase

File Contents

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