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