ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/java/lang/System.java
Revision: 1.4
Committed: Thu Jan 9 17:56:28 2003 UTC (21 years, 5 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +0 -0 lines
State: FILE REMOVED
Log Message:
Removed

File Contents

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