2
* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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).
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.
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
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;
37
import sun.reflect.Reflection;
38
import sun.security.util.SecurityConstants;
43
static InputStream in = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
44
static PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out), 128), true);
45
static PrintStream err = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err), 128), true);
52
static Properties props;
53
static String lineSeparator;
57
props = new Properties();
58
VMSystemProperties.initProperties(props);
59
lineSeparator = props.getProperty("line.separator");
61
// after we've initialized the system properties, we need to fixate certain
62
// results that depend on system properties, because we don't want Java code to
63
// be able to change the behavior by setting these system properties.
64
ClassLoader.initializeLibraryPaths(props);
65
sun.misc.VM.saveAndRemoveProperties(props);
67
// now that we've initialized the system properties (which are our only
68
// notion of "booting" the VM) we set the booted flag.
74
* The <code>System</code> class contains several useful class fields
75
* and methods. It cannot be instantiated.
77
* <p>Among the facilities provided by the <code>System</code> class
78
* are standard input, standard output, and error output streams;
79
* access to externally defined properties and environment
80
* variables; a means of loading files and libraries; and a utility
81
* method for quickly copying a portion of an array.
86
public final class System {
88
/** Don't let anyone instantiate this class */
93
* The "standard" input stream. This stream is already
94
* open and ready to supply input data. Typically this stream
95
* corresponds to keyboard input or another input source specified by
96
* the host environment or user.
98
@ikvm.lang.Property(get="get_in")
99
public final static InputStream in;
101
static { in = null; }
103
private static InputStream get_in()
109
* The "standard" output stream. This stream is already
110
* open and ready to accept output data. Typically this stream
111
* corresponds to display output or another output destination
112
* specified by the host environment or user.
114
* For simple stand-alone Java applications, a typical way to write
115
* a line of output data is:
117
* System.out.println(data)
118
* </pre></blockquote>
120
* See the <code>println</code> methods in class <code>PrintStream</code>.
122
* @see java.io.PrintStream#println()
123
* @see java.io.PrintStream#println(boolean)
124
* @see java.io.PrintStream#println(char)
125
* @see java.io.PrintStream#println(char[])
126
* @see java.io.PrintStream#println(double)
127
* @see java.io.PrintStream#println(float)
128
* @see java.io.PrintStream#println(int)
129
* @see java.io.PrintStream#println(long)
130
* @see java.io.PrintStream#println(java.lang.Object)
131
* @see java.io.PrintStream#println(java.lang.String)
133
@ikvm.lang.Property(get="get_out")
134
public final static PrintStream out;
136
static { out = null; }
138
private static PrintStream get_out()
144
* The "standard" error output stream. This stream is already
145
* open and ready to accept output data.
147
* Typically this stream corresponds to display output or another
148
* output destination specified by the host environment or user. By
149
* convention, this output stream is used to display error messages
150
* or other information that should come to the immediate attention
151
* of a user even if the principal output stream, the value of the
152
* variable <code>out</code>, has been redirected to a file or other
153
* destination that is typically not continuously monitored.
155
@ikvm.lang.Property(get="get_err")
156
public final static PrintStream err;
158
static { err = null ; }
160
private static PrintStream get_err()
165
/* The security manager for the system.
167
private static volatile SecurityManager security;
170
* Reassigns the "standard" input stream.
172
* <p>First, if there is a security manager, its <code>checkPermission</code>
173
* method is called with a <code>RuntimePermission("setIO")</code> permission
174
* to see if it's ok to reassign the "standard" input stream.
177
* @param in the new standard input stream.
179
* @throws SecurityException
180
* if a security manager exists and its
181
* <code>checkPermission</code> method doesn't allow
182
* reassigning of the standard input stream.
184
* @see SecurityManager#checkPermission
185
* @see java.lang.RuntimePermission
189
public static void setIn(InputStream in) {
195
* Reassigns the "standard" output stream.
197
* <p>First, if there is a security manager, its <code>checkPermission</code>
198
* method is called with a <code>RuntimePermission("setIO")</code> permission
199
* to see if it's ok to reassign the "standard" output stream.
201
* @param out the new standard output stream
203
* @throws SecurityException
204
* if a security manager exists and its
205
* <code>checkPermission</code> method doesn't allow
206
* reassigning of the standard output stream.
208
* @see SecurityManager#checkPermission
209
* @see java.lang.RuntimePermission
213
public static void setOut(PrintStream out) {
219
* Reassigns the "standard" error output stream.
221
* <p>First, if there is a security manager, its <code>checkPermission</code>
222
* method is called with a <code>RuntimePermission("setIO")</code> permission
223
* to see if it's ok to reassign the "standard" error output stream.
225
* @param err the new standard error output stream.
227
* @throws SecurityException
228
* if a security manager exists and its
229
* <code>checkPermission</code> method doesn't allow
230
* reassigning of the standard error output stream.
232
* @see SecurityManager#checkPermission
233
* @see java.lang.RuntimePermission
237
public static void setErr(PrintStream err) {
242
private static volatile Console cons;
244
* Returns the unique {@link java.io.Console Console} object associated
245
* with the current Java virtual machine, if any.
247
* @return The system console, if any, otherwise <tt>null</tt>.
251
public static Console console() {
253
synchronized (System.class) {
254
cons = sun.misc.SharedSecrets.getJavaIOAccess().console();
261
* Returns the channel inherited from the entity that created this
262
* Java virtual machine.
264
* <p> This method returns the channel obtained by invoking the
265
* {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
266
* inheritedChannel} method of the system-wide default
267
* {@link java.nio.channels.spi.SelectorProvider} object. </p>
269
* <p> In addition to the network-oriented channels described in
270
* {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
271
* inheritedChannel}, this method may return other kinds of
272
* channels in the future.
274
* @return The inherited channel, if any, otherwise <tt>null</tt>.
276
* @throws IOException
277
* If an I/O error occurs
279
* @throws SecurityException
280
* If a security manager is present and it does not
281
* permit access to the channel.
285
public static Channel inheritedChannel() throws IOException {
286
return SelectorProvider.provider().inheritedChannel();
289
private static void checkIO() {
290
SecurityManager sm = getSecurityManager();
292
sm.checkPermission(new RuntimePermission("setIO"));
297
* Sets the System security.
299
* <p> If there is a security manager already installed, this method first
300
* calls the security manager's <code>checkPermission</code> method
301
* with a <code>RuntimePermission("setSecurityManager")</code>
302
* permission to ensure it's ok to replace the existing
304
* This may result in throwing a <code>SecurityException</code>.
306
* <p> Otherwise, the argument is established as the current
307
* security manager. If the argument is <code>null</code> and no
308
* security manager has been established, then no action is taken and
309
* the method simply returns.
311
* @param s the security manager.
312
* @exception SecurityException if the security manager has already
313
* been set and its <code>checkPermission</code> method
314
* doesn't allow it to be replaced.
315
* @see #getSecurityManager
316
* @see SecurityManager#checkPermission
317
* @see java.lang.RuntimePermission
320
void setSecurityManager(final SecurityManager s) {
322
s.checkPackageAccess("java.lang");
323
} catch (Exception e) {
326
setSecurityManager0(s);
329
private static synchronized
330
void setSecurityManager0(final SecurityManager s) {
331
// [IKVM] force sun.misc.Launcher to initialize, because it assumes that it runs without a SecurityManager
332
sun.misc.Launcher.getLauncher();
334
SecurityManager sm = getSecurityManager();
336
// ask the currently installed security manager if we
338
sm.checkPermission(new RuntimePermission
339
("setSecurityManager"));
342
if ((s != null) && (s.getClass().getClassLoader() != null)) {
343
// New security manager class is not on bootstrap classpath.
344
// Cause policy to get initialized before we install the new
345
// security manager, in order to prevent infinite loops when
346
// trying to initialize the policy (which usually involves
347
// accessing some security and/or system properties, which in turn
348
// calls the installed security manager's checkPermission method
349
// which will loop infinitely if there is a non-system class
350
// (in this case: the new security manager class) on the stack).
351
AccessController.doPrivileged(new PrivilegedAction<Object>() {
352
public Object run() {
353
s.getClass().getProtectionDomain().implies
354
(SecurityConstants.ALL_PERMISSION);
364
* Gets the system security interface.
366
* @return if a security manager has already been established for the
367
* current application, then that security manager is returned;
368
* otherwise, <code>null</code> is returned.
369
* @see #setSecurityManager
371
public static SecurityManager getSecurityManager() {
376
* Returns the current time in milliseconds. Note that
377
* while the unit of time of the return value is a millisecond,
378
* the granularity of the value depends on the underlying
379
* operating system and may be larger. For example, many
380
* operating systems measure time in units of tens of
383
* <p> See the description of the class <code>Date</code> for
384
* a discussion of slight discrepancies that may arise between
385
* "computer time" and coordinated universal time (UTC).
387
* @return the difference, measured in milliseconds, between
388
* the current time and midnight, January 1, 1970 UTC.
389
* @see java.util.Date
391
public static long currentTimeMillis() {
392
long january_1st_1970 = 62135596800000L;
393
return cli.System.DateTime.get_UtcNow().get_Ticks() / 10000L - january_1st_1970;
397
* Returns the current value of the running Java Virtual Machine's
398
* high-resolution time source, in nanoseconds.
400
* <p>This method can only be used to measure elapsed time and is
401
* not related to any other notion of system or wall-clock time.
402
* The value returned represents nanoseconds since some fixed but
403
* arbitrary <i>origin</i> time (perhaps in the future, so values
404
* may be negative). The same origin is used by all invocations of
405
* this method in an instance of a Java virtual machine; other
406
* virtual machine instances are likely to use a different origin.
408
* <p>This method provides nanosecond precision, but not necessarily
409
* nanosecond resolution (that is, how frequently the value changes)
410
* - no guarantees are made except that the resolution is at least as
411
* good as that of {@link #currentTimeMillis()}.
413
* <p>Differences in successive calls that span greater than
414
* approximately 292 years (2<sup>63</sup> nanoseconds) will not
415
* correctly compute elapsed time due to numerical overflow.
417
* <p>The values returned by this method become meaningful only when
418
* the difference between two such values, obtained within the same
419
* instance of a Java virtual machine, is computed.
421
* <p> For example, to measure how long some code takes to execute:
423
* long startTime = System.nanoTime();
424
* // ... the code being measured ...
425
* long estimatedTime = System.nanoTime() - startTime;}</pre>
427
* <p>To compare two nanoTime values
429
* long t0 = System.nanoTime();
431
* long t1 = System.nanoTime();}</pre>
433
* one should use {@code t1 - t0 < 0}, not {@code t1 < t0},
434
* because of the possibility of numerical overflow.
436
* @return the current value of the running Java Virtual Machine's
437
* high-resolution time source, in nanoseconds
440
public static long nanoTime() {
441
long NANOS_PER_SEC = 1000000000;
442
double current = cli.System.Diagnostics.Stopwatch.GetTimestamp();
443
double freq = cli.System.Diagnostics.Stopwatch.Frequency;
444
return (long)((current / freq) * NANOS_PER_SEC);
448
* Copies an array from the specified source array, beginning at the
449
* specified position, to the specified position of the destination array.
450
* A subsequence of array components are copied from the source
451
* array referenced by <code>src</code> to the destination array
452
* referenced by <code>dest</code>. The number of components copied is
453
* equal to the <code>length</code> argument. The components at
454
* positions <code>srcPos</code> through
455
* <code>srcPos+length-1</code> in the source array are copied into
456
* positions <code>destPos</code> through
457
* <code>destPos+length-1</code>, respectively, of the destination
460
* If the <code>src</code> and <code>dest</code> arguments refer to the
461
* same array object, then the copying is performed as if the
462
* components at positions <code>srcPos</code> through
463
* <code>srcPos+length-1</code> were first copied to a temporary
464
* array with <code>length</code> components and then the contents of
465
* the temporary array were copied into positions
466
* <code>destPos</code> through <code>destPos+length-1</code> of the
469
* If <code>dest</code> is <code>null</code>, then a
470
* <code>NullPointerException</code> is thrown.
472
* If <code>src</code> is <code>null</code>, then a
473
* <code>NullPointerException</code> is thrown and the destination
474
* array is not modified.
476
* Otherwise, if any of the following is true, an
477
* <code>ArrayStoreException</code> is thrown and the destination is
480
* <li>The <code>src</code> argument refers to an object that is not an
482
* <li>The <code>dest</code> argument refers to an object that is not an
484
* <li>The <code>src</code> argument and <code>dest</code> argument refer
485
* to arrays whose component types are different primitive types.
486
* <li>The <code>src</code> argument refers to an array with a primitive
487
* component type and the <code>dest</code> argument refers to an array
488
* with a reference component type.
489
* <li>The <code>src</code> argument refers to an array with a reference
490
* component type and the <code>dest</code> argument refers to an array
491
* with a primitive component type.
494
* Otherwise, if any of the following is true, an
495
* <code>IndexOutOfBoundsException</code> is
496
* thrown and the destination is not modified:
498
* <li>The <code>srcPos</code> argument is negative.
499
* <li>The <code>destPos</code> argument is negative.
500
* <li>The <code>length</code> argument is negative.
501
* <li><code>srcPos+length</code> is greater than
502
* <code>src.length</code>, the length of the source array.
503
* <li><code>destPos+length</code> is greater than
504
* <code>dest.length</code>, the length of the destination array.
507
* Otherwise, if any actual component of the source array from
508
* position <code>srcPos</code> through
509
* <code>srcPos+length-1</code> cannot be converted to the component
510
* type of the destination array by assignment conversion, an
511
* <code>ArrayStoreException</code> is thrown. In this case, let
512
* <b><i>k</i></b> be the smallest nonnegative integer less than
513
* length such that <code>src[srcPos+</code><i>k</i><code>]</code>
514
* cannot be converted to the component type of the destination
515
* array; when the exception is thrown, source array components from
516
* positions <code>srcPos</code> through
517
* <code>srcPos+</code><i>k</i><code>-1</code>
518
* will already have been copied to destination array positions
519
* <code>destPos</code> through
520
* <code>destPos+</code><i>k</I><code>-1</code> and no other
521
* positions of the destination array will have been modified.
522
* (Because of the restrictions already itemized, this
523
* paragraph effectively applies only to the situation where both
524
* arrays have component types that are reference types.)
526
* @param src the source array.
527
* @param srcPos starting position in the source array.
528
* @param dest the destination array.
529
* @param destPos starting position in the destination data.
530
* @param length the number of array elements to be copied.
531
* @exception IndexOutOfBoundsException if copying would cause
532
* access of data outside array bounds.
533
* @exception ArrayStoreException if an element in the <code>src</code>
534
* array could not be stored into the <code>dest</code> array
535
* because of a type mismatch.
536
* @exception NullPointerException if either <code>src</code> or
537
* <code>dest</code> is <code>null</code>.
539
public static native void arraycopy(Object src, int srcPos,
540
Object dest, int destPos,
544
* Returns the same hash code for the given object as
545
* would be returned by the default method hashCode(),
546
* whether or not the given object's class overrides
548
* The hash code for the null reference is zero.
550
* @param x object for which the hashCode is to be calculated
551
* @return the hashCode
554
public static int identityHashCode(Object x) {
555
return cli.System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(x);
559
* System properties. The following properties are guaranteed to be defined:
561
* <dt>java.version <dd>Java version number
562
* <dt>java.vendor <dd>Java vendor specific string
563
* <dt>java.vendor.url <dd>Java vendor URL
564
* <dt>java.home <dd>Java installation directory
565
* <dt>java.class.version <dd>Java class version number
566
* <dt>java.class.path <dd>Java classpath
567
* <dt>os.name <dd>Operating System Name
568
* <dt>os.arch <dd>Operating System Architecture
569
* <dt>os.version <dd>Operating System Version
570
* <dt>file.separator <dd>File separator ("/" on Unix)
571
* <dt>path.separator <dd>Path separator (":" on Unix)
572
* <dt>line.separator <dd>Line separator ("\n" on Unix)
573
* <dt>user.name <dd>User account name
574
* <dt>user.home <dd>User home directory
575
* <dt>user.dir <dd>User's current working directory
579
//private static native Properties initProperties(Properties props);
582
* Determines the current system properties.
584
* First, if there is a security manager, its
585
* <code>checkPropertiesAccess</code> method is called with no
586
* arguments. This may result in a security exception.
588
* The current set of system properties for use by the
589
* {@link #getProperty(String)} method is returned as a
590
* <code>Properties</code> object. If there is no current set of
591
* system properties, a set of system properties is first created and
592
* initialized. This set of system properties always includes values
593
* for the following keys:
594
* <table summary="Shows property keys and associated values">
596
* <th>Description of Associated Value</th></tr>
597
* <tr><td><code>java.version</code></td>
598
* <td>Java Runtime Environment version</td></tr>
599
* <tr><td><code>java.vendor</code></td>
600
* <td>Java Runtime Environment vendor</td></tr
601
* <tr><td><code>java.vendor.url</code></td>
602
* <td>Java vendor URL</td></tr>
603
* <tr><td><code>java.home</code></td>
604
* <td>Java installation directory</td></tr>
605
* <tr><td><code>java.vm.specification.version</code></td>
606
* <td>Java Virtual Machine specification version</td></tr>
607
* <tr><td><code>java.vm.specification.vendor</code></td>
608
* <td>Java Virtual Machine specification vendor</td></tr>
609
* <tr><td><code>java.vm.specification.name</code></td>
610
* <td>Java Virtual Machine specification name</td></tr>
611
* <tr><td><code>java.vm.version</code></td>
612
* <td>Java Virtual Machine implementation version</td></tr>
613
* <tr><td><code>java.vm.vendor</code></td>
614
* <td>Java Virtual Machine implementation vendor</td></tr>
615
* <tr><td><code>java.vm.name</code></td>
616
* <td>Java Virtual Machine implementation name</td></tr>
617
* <tr><td><code>java.specification.version</code></td>
618
* <td>Java Runtime Environment specification version</td></tr>
619
* <tr><td><code>java.specification.vendor</code></td>
620
* <td>Java Runtime Environment specification vendor</td></tr>
621
* <tr><td><code>java.specification.name</code></td>
622
* <td>Java Runtime Environment specification name</td></tr>
623
* <tr><td><code>java.class.version</code></td>
624
* <td>Java class format version number</td></tr>
625
* <tr><td><code>java.class.path</code></td>
626
* <td>Java class path</td></tr>
627
* <tr><td><code>java.library.path</code></td>
628
* <td>List of paths to search when loading libraries</td></tr>
629
* <tr><td><code>java.io.tmpdir</code></td>
630
* <td>Default temp file path</td></tr>
631
* <tr><td><code>java.compiler</code></td>
632
* <td>Name of JIT compiler to use</td></tr>
633
* <tr><td><code>java.ext.dirs</code></td>
634
* <td>Path of extension directory or directories</td></tr>
635
* <tr><td><code>os.name</code></td>
636
* <td>Operating system name</td></tr>
637
* <tr><td><code>os.arch</code></td>
638
* <td>Operating system architecture</td></tr>
639
* <tr><td><code>os.version</code></td>
640
* <td>Operating system version</td></tr>
641
* <tr><td><code>file.separator</code></td>
642
* <td>File separator ("/" on UNIX)</td></tr>
643
* <tr><td><code>path.separator</code></td>
644
* <td>Path separator (":" on UNIX)</td></tr>
645
* <tr><td><code>line.separator</code></td>
646
* <td>Line separator ("\n" on UNIX)</td></tr>
647
* <tr><td><code>user.name</code></td>
648
* <td>User's account name</td></tr>
649
* <tr><td><code>user.home</code></td>
650
* <td>User's home directory</td></tr>
651
* <tr><td><code>user.dir</code></td>
652
* <td>User's current working directory</td></tr>
655
* Multiple paths in a system property value are separated by the path
656
* separator character of the platform.
658
* Note that even if the security manager does not permit the
659
* <code>getProperties</code> operation, it may choose to permit the
660
* {@link #getProperty(String)} operation.
662
* @return the system properties
663
* @exception SecurityException if a security manager exists and its
664
* <code>checkPropertiesAccess</code> method doesn't allow access
665
* to the system properties.
666
* @see #setProperties
667
* @see java.lang.SecurityException
668
* @see java.lang.SecurityManager#checkPropertiesAccess()
669
* @see java.util.Properties
671
public static Properties getProperties() {
672
SecurityManager sm = getSecurityManager();
674
sm.checkPropertiesAccess();
681
* Returns the system-dependent line separator string. It always
682
* returns the same value - the initial value of the {@linkplain
683
* #getProperty(String) system property} {@code line.separator}.
685
* <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
686
* Windows systems it returns {@code "\r\n"}.
688
public static String lineSeparator() {
689
return Props.lineSeparator;
693
* Sets the system properties to the <code>Properties</code>
696
* First, if there is a security manager, its
697
* <code>checkPropertiesAccess</code> method is called with no
698
* arguments. This may result in a security exception.
700
* The argument becomes the current set of system properties for use
701
* by the {@link #getProperty(String)} method. If the argument is
702
* <code>null</code>, then the current set of system properties is
705
* @param props the new system properties.
706
* @exception SecurityException if a security manager exists and its
707
* <code>checkPropertiesAccess</code> method doesn't allow access
708
* to the system properties.
709
* @see #getProperties
710
* @see java.util.Properties
711
* @see java.lang.SecurityException
712
* @see java.lang.SecurityManager#checkPropertiesAccess()
714
public static void setProperties(Properties props) {
715
SecurityManager sm = getSecurityManager();
717
sm.checkPropertiesAccess();
720
props = new Properties();
721
VMSystemProperties.initProperties(props);
727
* Gets the system property indicated by the specified key.
729
* First, if there is a security manager, its
730
* <code>checkPropertyAccess</code> method is called with the key as
731
* its argument. This may result in a SecurityException.
733
* If there is no current set of system properties, a set of system
734
* properties is first created and initialized in the same manner as
735
* for the <code>getProperties</code> method.
737
* @param key the name of the system property.
738
* @return the string value of the system property,
739
* or <code>null</code> if there is no property with that key.
741
* @exception SecurityException if a security manager exists and its
742
* <code>checkPropertyAccess</code> method doesn't allow
743
* access to the specified system property.
744
* @exception NullPointerException if <code>key</code> is
746
* @exception IllegalArgumentException if <code>key</code> is empty.
748
* @see java.lang.SecurityException
749
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
750
* @see java.lang.System#getProperties()
752
public static String getProperty(String key) {
754
SecurityManager sm = getSecurityManager();
756
sm.checkPropertyAccess(key);
759
return Props.props.getProperty(key);
763
* Gets the system property indicated by the specified key.
765
* First, if there is a security manager, its
766
* <code>checkPropertyAccess</code> method is called with the
767
* <code>key</code> as its argument.
769
* If there is no current set of system properties, a set of system
770
* properties is first created and initialized in the same manner as
771
* for the <code>getProperties</code> method.
773
* @param key the name of the system property.
774
* @param def a default value.
775
* @return the string value of the system property,
776
* or the default value if there is no property with that key.
778
* @exception SecurityException if a security manager exists and its
779
* <code>checkPropertyAccess</code> method doesn't allow
780
* access to the specified system property.
781
* @exception NullPointerException if <code>key</code> is
783
* @exception IllegalArgumentException if <code>key</code> is empty.
785
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
786
* @see java.lang.System#getProperties()
788
public static String getProperty(String key, String def) {
790
SecurityManager sm = getSecurityManager();
792
sm.checkPropertyAccess(key);
795
return Props.props.getProperty(key, def);
799
* Sets the system property indicated by the specified key.
801
* First, if a security manager exists, its
802
* <code>SecurityManager.checkPermission</code> method
803
* is called with a <code>PropertyPermission(key, "write")</code>
804
* permission. This may result in a SecurityException being thrown.
805
* If no exception is thrown, the specified property is set to the given
809
* @param key the name of the system property.
810
* @param value the value of the system property.
811
* @return the previous value of the system property,
812
* or <code>null</code> if it did not have one.
814
* @exception SecurityException if a security manager exists and its
815
* <code>checkPermission</code> method doesn't allow
816
* setting of the specified property.
817
* @exception NullPointerException if <code>key</code> or
818
* <code>value</code> is <code>null</code>.
819
* @exception IllegalArgumentException if <code>key</code> is empty.
821
* @see java.lang.System#getProperty(java.lang.String)
822
* @see java.lang.System#getProperty(java.lang.String, java.lang.String)
823
* @see java.util.PropertyPermission
824
* @see SecurityManager#checkPermission
827
public static String setProperty(String key, String value) {
829
SecurityManager sm = getSecurityManager();
831
sm.checkPermission(new PropertyPermission(key,
832
SecurityConstants.PROPERTY_WRITE_ACTION));
835
return (String) Props.props.setProperty(key, value);
839
* Removes the system property indicated by the specified key.
841
* First, if a security manager exists, its
842
* <code>SecurityManager.checkPermission</code> method
843
* is called with a <code>PropertyPermission(key, "write")</code>
844
* permission. This may result in a SecurityException being thrown.
845
* If no exception is thrown, the specified property is removed.
848
* @param key the name of the system property to be removed.
849
* @return the previous string value of the system property,
850
* or <code>null</code> if there was no property with that key.
852
* @exception SecurityException if a security manager exists and its
853
* <code>checkPropertyAccess</code> method doesn't allow
854
* access to the specified system property.
855
* @exception NullPointerException if <code>key</code> is
857
* @exception IllegalArgumentException if <code>key</code> is empty.
860
* @see java.util.Properties
861
* @see java.lang.SecurityException
862
* @see java.lang.SecurityManager#checkPropertiesAccess()
865
public static String clearProperty(String key) {
867
SecurityManager sm = getSecurityManager();
869
sm.checkPermission(new PropertyPermission(key, "write"));
872
return (String) Props.props.remove(key);
875
private static void checkKey(String key) {
877
throw new NullPointerException("key can't be null");
879
if (key.equals("")) {
880
throw new IllegalArgumentException("key can't be empty");
885
* Gets the value of the specified environment variable. An
886
* environment variable is a system-dependent external named
889
* <p>If a security manager exists, its
890
* {@link SecurityManager#checkPermission checkPermission}
891
* method is called with a
892
* <code>{@link RuntimePermission}("getenv."+name)</code>
893
* permission. This may result in a {@link SecurityException}
894
* being thrown. If no exception is thrown the value of the
895
* variable <code>name</code> is returned.
897
* <p><a name="EnvironmentVSSystemProperties"><i>System
898
* properties</i> and <i>environment variables</i></a> are both
899
* conceptually mappings between names and values. Both
900
* mechanisms can be used to pass user-defined information to a
901
* Java process. Environment variables have a more global effect,
902
* because they are visible to all descendants of the process
903
* which defines them, not just the immediate Java subprocess.
904
* They can have subtly different semantics, such as case
905
* insensitivity, on different operating systems. For these
906
* reasons, environment variables are more likely to have
907
* unintended side effects. It is best to use system properties
908
* where possible. Environment variables should be used when a
909
* global effect is desired, or when an external system interface
910
* requires an environment variable (such as <code>PATH</code>).
912
* <p>On UNIX systems the alphabetic case of <code>name</code> is
913
* typically significant, while on Microsoft Windows systems it is
914
* typically not. For example, the expression
915
* <code>System.getenv("FOO").equals(System.getenv("foo"))</code>
916
* is likely to be true on Microsoft Windows.
918
* @param name the name of the environment variable
919
* @return the string value of the variable, or <code>null</code>
920
* if the variable is not defined in the system environment
921
* @throws NullPointerException if <code>name</code> is <code>null</code>
922
* @throws SecurityException
923
* if a security manager exists and its
924
* {@link SecurityManager#checkPermission checkPermission}
925
* method doesn't allow access to the environment variable
928
* @see ProcessBuilder#environment()
930
public static String getenv(String name) {
931
SecurityManager sm = getSecurityManager();
933
sm.checkPermission(new RuntimePermission("getenv."+name));
936
return ProcessEnvironment.getenv(name);
941
* Returns an unmodifiable string map view of the current system environment.
942
* The environment is a system-dependent mapping from names to
943
* values which is passed from parent to child processes.
945
* <p>If the system does not support environment variables, an
946
* empty map is returned.
948
* <p>The returned map will never contain null keys or values.
949
* Attempting to query the presence of a null key or value will
950
* throw a {@link NullPointerException}. Attempting to query
951
* the presence of a key or value which is not of type
952
* {@link String} will throw a {@link ClassCastException}.
954
* <p>The returned map and its collection views may not obey the
955
* general contract of the {@link Object#equals} and
956
* {@link Object#hashCode} methods.
958
* <p>The returned map is typically case-sensitive on all platforms.
960
* <p>If a security manager exists, its
961
* {@link SecurityManager#checkPermission checkPermission}
962
* method is called with a
963
* <code>{@link RuntimePermission}("getenv.*")</code>
964
* permission. This may result in a {@link SecurityException} being
967
* <p>When passing information to a Java subprocess,
968
* <a href=#EnvironmentVSSystemProperties>system properties</a>
969
* are generally preferred over environment variables.
971
* @return the environment as a map of variable names to values
972
* @throws SecurityException
973
* if a security manager exists and its
974
* {@link SecurityManager#checkPermission checkPermission}
975
* method doesn't allow access to the process environment
976
* @see #getenv(String)
977
* @see ProcessBuilder#environment()
980
public static java.util.Map<String,String> getenv() {
981
SecurityManager sm = getSecurityManager();
983
sm.checkPermission(new RuntimePermission("getenv.*"));
986
return ProcessEnvironment.getenv();
990
* Terminates the currently running Java Virtual Machine. The
991
* argument serves as a status code; by convention, a nonzero status
992
* code indicates abnormal termination.
994
* This method calls the <code>exit</code> method in class
995
* <code>Runtime</code>. This method never returns normally.
997
* The call <code>System.exit(n)</code> is effectively equivalent to
1000
* Runtime.getRuntime().exit(n)
1001
* </pre></blockquote>
1003
* @param status exit status.
1004
* @throws SecurityException
1005
* if a security manager exists and its <code>checkExit</code>
1006
* method doesn't allow exit with the specified status.
1007
* @see java.lang.Runtime#exit(int)
1009
public static void exit(int status) {
1010
Runtime.getRuntime().exit(status);
1014
* Runs the garbage collector.
1016
* Calling the <code>gc</code> method suggests that the Java Virtual
1017
* Machine expend effort toward recycling unused objects in order to
1018
* make the memory they currently occupy available for quick reuse.
1019
* When control returns from the method call, the Java Virtual
1020
* Machine has made a best effort to reclaim space from all discarded
1023
* The call <code>System.gc()</code> is effectively equivalent to the
1026
* Runtime.getRuntime().gc()
1027
* </pre></blockquote>
1029
* @see java.lang.Runtime#gc()
1031
public static void gc() {
1032
Runtime.getRuntime().gc();
1036
* Runs the finalization methods of any objects pending finalization.
1038
* Calling this method suggests that the Java Virtual Machine expend
1039
* effort toward running the <code>finalize</code> methods of objects
1040
* that have been found to be discarded but whose <code>finalize</code>
1041
* methods have not yet been run. When control returns from the
1042
* method call, the Java Virtual Machine has made a best effort to
1043
* complete all outstanding finalizations.
1045
* The call <code>System.runFinalization()</code> is effectively
1046
* equivalent to the call:
1048
* Runtime.getRuntime().runFinalization()
1049
* </pre></blockquote>
1051
* @see java.lang.Runtime#runFinalization()
1053
public static void runFinalization() {
1054
Runtime.getRuntime().runFinalization();
1058
* Enable or disable finalization on exit; doing so specifies that the
1059
* finalizers of all objects that have finalizers that have not yet been
1060
* automatically invoked are to be run before the Java runtime exits.
1061
* By default, finalization on exit is disabled.
1063
* <p>If there is a security manager,
1064
* its <code>checkExit</code> method is first called
1065
* with 0 as its argument to ensure the exit is allowed.
1066
* This could result in a SecurityException.
1068
* @deprecated This method is inherently unsafe. It may result in
1069
* finalizers being called on live objects while other threads are
1070
* concurrently manipulating those objects, resulting in erratic
1071
* behavior or deadlock.
1072
* @param value indicating enabling or disabling of finalization
1073
* @throws SecurityException
1074
* if a security manager exists and its <code>checkExit</code>
1075
* method doesn't allow the exit.
1077
* @see java.lang.Runtime#exit(int)
1078
* @see java.lang.Runtime#gc()
1079
* @see java.lang.SecurityManager#checkExit(int)
1083
public static void runFinalizersOnExit(boolean value) {
1084
Runtime.getRuntime().runFinalizersOnExit(value);
1088
* Loads a code file with the specified filename from the local file
1089
* system as a dynamic library. The filename
1090
* argument must be a complete path name.
1092
* The call <code>System.load(name)</code> is effectively equivalent
1095
* Runtime.getRuntime().load(name)
1096
* </pre></blockquote>
1098
* @param filename the file to load.
1099
* @exception SecurityException if a security manager exists and its
1100
* <code>checkLink</code> method doesn't allow
1101
* loading of the specified dynamic library
1102
* @exception UnsatisfiedLinkError if the file does not exist.
1103
* @exception NullPointerException if <code>filename</code> is
1105
* @see java.lang.Runtime#load(java.lang.String)
1106
* @see java.lang.SecurityManager#checkLink(java.lang.String)
1108
@ikvm.internal.HasCallerID
1109
public static void load(String filename) {
1110
Runtime.getRuntime().load0(Reflection.getCallerClass(2), filename);
1114
* Loads the system library specified by the <code>libname</code>
1115
* argument. The manner in which a library name is mapped to the
1116
* actual system library is system dependent.
1118
* The call <code>System.loadLibrary(name)</code> is effectively
1119
* equivalent to the call
1121
* Runtime.getRuntime().loadLibrary(name)
1122
* </pre></blockquote>
1124
* @param libname the name of the library.
1125
* @exception SecurityException if a security manager exists and its
1126
* <code>checkLink</code> method doesn't allow
1127
* loading of the specified dynamic library
1128
* @exception UnsatisfiedLinkError if the library does not exist.
1129
* @exception NullPointerException if <code>libname</code> is
1131
* @see java.lang.Runtime#loadLibrary(java.lang.String)
1132
* @see java.lang.SecurityManager#checkLink(java.lang.String)
1134
@ikvm.internal.HasCallerID
1135
public static void loadLibrary(String libname) {
1136
Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(2), libname);
1140
* Maps a library name into a platform-specific string representing
1143
* @param libname the name of the library.
1144
* @return a platform-dependent native library name.
1145
* @exception NullPointerException if <code>libname</code> is
1147
* @see java.lang.System#loadLibrary(java.lang.String)
1148
* @see java.lang.ClassLoader#findLibrary(java.lang.String)
1151
public static String mapLibraryName(String libname) {
1152
if (libname == null) {
1153
throw new NullPointerException();
1155
if (ikvm.internal.Util.WINDOWS) {
1156
return libname + ".dll";
1157
} else if (ikvm.internal.Util.MACOSX) {
1158
return "lib" + libname + ".jnilib";
1160
return "lib" + libname + ".so";
1164
/* returns the class of the caller. */
1165
static Class<?> getCallerClass() {
1166
// NOTE use of more generic Reflection.getCallerClass()
1167
return Reflection.getCallerClass(3);