system class of java

Upload: simanta-sahu

Post on 07-Apr-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 System Class of Java

    1/25

    Save This Page

    Home openjdk-7 java lang [javadoc | source]1 /*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.

    4 *5 * This code is free software; you can redistribute it and/or modify

    it6 * under the terms of the GNU General Public License version 2 only,

    as7 * published by the Free Software Foundation. Oracle designates this8 * particular file as subject to the "Classpath" exception as provided9 * by Oracle in the LICENSE file that accompanied this code.

    10 *11 * This code is distributed in the hope that it will be useful, but

    WITHOUT12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY

    or13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public

    License14 * version 2 for more details (a copy is included in the LICENSE file

    that15 * accompanied this code).16 *17 * You should have received a copy of the GNU General Public License

    version18 * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA

    94065 USA

    22 * or visit www.oracle.com if you need additional information or haveany

    23 * questions.24 */25 package java.lang;2627 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;4041 /**42 * The System class contains several useful class fields43 * and methods. It cannot be instantiated.

    http://www.docjar.com/http://www.docjar.com/projects/openjdk-7-java.htmlhttp://www.docjar.com/projects/openjdk-7-java.htmlhttp://www.docjar.com/docs/api/java/lang/package-index.htmlhttp://www.docjar.com/docs/api/java/lang/System.htmlhttp://www.docjar.com/docs/api/java/lang/package-index.htmlhttp://www.docjar.com/docs/api/java/io/package-index.htmlhttp://www.docjar.com/docs/api/java/util/Properties.htmlhttp://www.docjar.com/docs/api/java/util/PropertyPermission.htmlhttp://www.docjar.com/docs/api/java/util/StringTokenizer.htmlhttp://www.docjar.com/docs/api/java/security/AccessController.htmlhttp://www.docjar.com/docs/api/java/security/PrivilegedAction.htmlhttp://www.docjar.com/docs/api/java/security/AllPermission.htmlhttp://www.docjar.com/docs/api/java/nio/channels/Channel.htmlhttp://www.docjar.com/docs/api/java/nio/channels/spi/SelectorProvider.htmlhttp://www.docjar.com/docs/api/sun/nio/ch/Interruptible.htmlhttp://www.docjar.com/docs/api/sun/reflect/Reflection.htmlhttp://www.docjar.com/docs/api/sun/security/util/SecurityConstants.htmlhttp://www.docjar.com/docs/api/sun/reflect/annotation/AnnotationType.htmlhttp://del.icio.us/posthttp://www.docjar.com/http://www.docjar.com/projects/openjdk-7-java.htmlhttp://www.docjar.com/docs/api/java/lang/package-index.htmlhttp://www.docjar.com/docs/api/java/lang/System.htmlhttp://www.docjar.com/docs/api/java/lang/package-index.htmlhttp://www.docjar.com/docs/api/java/io/package-index.htmlhttp://www.docjar.com/docs/api/java/util/Properties.htmlhttp://www.docjar.com/docs/api/java/util/PropertyPermission.htmlhttp://www.docjar.com/docs/api/java/util/StringTokenizer.htmlhttp://www.docjar.com/docs/api/java/security/AccessController.htmlhttp://www.docjar.com/docs/api/java/security/PrivilegedAction.htmlhttp://www.docjar.com/docs/api/java/security/AllPermission.htmlhttp://www.docjar.com/docs/api/java/nio/channels/Channel.htmlhttp://www.docjar.com/docs/api/java/nio/channels/spi/SelectorProvider.htmlhttp://www.docjar.com/docs/api/sun/nio/ch/Interruptible.htmlhttp://www.docjar.com/docs/api/sun/reflect/Reflection.htmlhttp://www.docjar.com/docs/api/sun/security/util/SecurityConstants.htmlhttp://www.docjar.com/docs/api/sun/reflect/annotation/AnnotationType.html
  • 8/4/2019 System Class of Java

    2/25

    44 *45 *

    Among the facilities provided by the System class46 * are standard input, standard output, and error output streams;47 * access to externally defined properties and environment48 * variables; a means of loading files and libraries; and a utility49 * method for quickly copying a portion of an array.50 *51 * @author unascribed52 * @since JDK1.053 */54 public final class System {5556 /* register the natives via the static initializer.57 *58 * VM will invoke the initializeSystemClass method to complete59 * the initialization for this class separated from clinit.60 * Note that to use properties set by the VM, see the constraints61 * described in the initializeSystemClass method.62 */63 private static native void registerNatives();

    64 static {65 registerNatives();66 }6768 /** Don't let anyone instantiate this class */69 private System() {70 }7172 /**73 * The "standard" input stream. This stream is already74 * open and ready to supply input data. Typically this stream75 * corresponds to keyboard input or another input source

    specified by76 * the host environment or user.77 */78 public final static InputStream in = null;7980 /**81 * The "standard" output stream. This stream is already82 * open and ready to accept output data. Typically this stream83 * corresponds to display output or another output destination84 * specified by the host environment or user.85 *

    86 * For simple stand-alone Java applications, a typical way to

    write87 * a line of output data is:88 *

    89 * System.out.println(data)90 * 91 *

    92 * See the println methods in class

    PrintStream.93 *94 * @see java.io.PrintStream#println()95 * @see java.io.PrintStream#println(boolean)96 * @see java.io.PrintStream#println(char)97 * @see java.io.PrintStream#println(char[])

  • 8/4/2019 System Class of Java

    3/25

    98 * @see java.io.PrintStream#println(double)99 * @see java.io.PrintStream#println(float)100 * @see java.io.PrintStream#println(int)101 * @see java.io.PrintStream#println(long)102 * @see java.io.PrintStream#println(java.lang.Object)103 * @see java.io.PrintStream#println(java.lang.String)104 */105 public final static PrintStream out = null;106107 /**108 * The "standard" error output stream. This stream is already109 * open and ready to accept output data.110 *

    111 * Typically this stream corresponds to display output or another112 * output destination specified by the host environment or user.

    By113 * convention, this output stream is used to display error

    messages114 * or other information that should come to the immediate

    attention

    115 * of a user even if the principal output stream, the value of the116 * variable out, has been redirected to a file or

    other117 * destination that is typically not continuously monitored.118 */119 public final static PrintStream err = null;120121 /* The security manager for the system.122 */123 private static volatile SecurityManager security = null;124125 /**126 * Reassigns the "standard" input stream.127 *128 *

    First, if there is a security manager, its

    checkPermission129 * method is called with a

    RuntimePermission("setIO") permission130 * to see if it's ok to reassign the "standard" input stream.131 *

    132 *133 * @param in the new standard input stream.134 *135 * @throws SecurityException136 * if a security manager exists and its137 * checkPermission method doesn't allow138 * reassigning of the standard input stream.

    139 *140 * @see SecurityManager#checkPermission141 * @see java.lang.RuntimePermission142 *143 * @since JDK1.1144 */145 public static void setIn(InputStream in) {146 checkIO();147 setIn0(in);148 }

  • 8/4/2019 System Class of Java

    4/25

    149150 /**151 * Reassigns the "standard" output stream.152 *153 *

    First, if there is a security manager, its

    checkPermission154 * method is called with a

    RuntimePermission("setIO") permission155 * to see if it's ok to reassign the "standard" output stream.156 *157 * @param out the new standard output stream158 *159 * @throws SecurityException160 * if a security manager exists and its161 * checkPermission method doesn't allow162 * reassigning of the standard output stream.163 *164 * @see SecurityManager#checkPermission165 * @see java.lang.RuntimePermission166 *

    167 * @since JDK1.1168 */169 public static void setOut(PrintStream out) {170 checkIO();171 setOut0(out);172 }173174 /**175 * Reassigns the "standard" error output stream.176 *177 *

    First, if there is a security manager, its

    checkPermission178 * method is called with a

    RuntimePermission("setIO") permission179 * to see if it's ok to reassign the "standard" error output

    stream.180 *181 * @param err the new standard error output stream.182 *183 * @throws SecurityException184 * if a security manager exists and its185 * checkPermission method doesn't allow186 * reassigning of the standard error output stream.187 *188 * @see SecurityManager#checkPermission189 * @see java.lang.RuntimePermission190 *

    191 * @since JDK1.1192 */193 public static void setErr(PrintStream err) {194 checkIO();195 setErr0(err);196 }197198 private static volatile Console cons = null;199 /**

  • 8/4/2019 System Class of Java

    5/25

    200 * Returns the unique {@link java.io.Console Console} objectassociated201 * with the current Java virtual machine, if any.202 *203 * @return The system console, if any, otherwise null.204 *205 * @since 1.6206 */207 public static Console console() {208 if (cons == null) {209 synchronized (System.class) {210 cons =

    sun.misc.SharedSecrets.getJavaIOAccess().console();211 }212 }213 return cons;214 }215216 /**217 * Returns the channel inherited from the entity that created this

    218 * Java virtual machine.219 *220 *

    This method returns the channel obtained by invoking the221 * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel222 * inheritedChannel} method of the system-wide default223 * {@link java.nio.channels.spi.SelectorProvider} object.

    224 *225 *

    In addition to the network-oriented channels described in226 * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel227 * inheritedChannel}, this method may return other kinds of228 * channels in the future.229 *230 * @return The inherited channel, if any, otherwise

    null.231 *232 * @throws IOException233 * If an I/O error occurs234 *235 * @throws SecurityException236 * If a security manager is present and it does not237 * permit access to the channel.238 *239 * @since 1.5240 */241 public static Channel inheritedChannel() throws IOException {242 return SelectorProvider.provider().inheritedChannel();243 }

    244245 private static void checkIO() {246 SecurityManager sm = getSecurityManager();247 if (sm != null) {248 sm.checkPermission(new RuntimePermission("setIO"));249 }250 }251252 private static native void setIn0(InputStream in);253 private static native void setOut0(PrintStream out);

  • 8/4/2019 System Class of Java

    6/25

    254 private static native void setErr0(PrintStream err);255256 /**257 * Sets the System security.258 *259 *

    If there is a security manager already installed, this

    method first260 * calls the security manager's checkPermission

    method261 * with a RuntimePermission("setSecurityManager")262 * permission to ensure it's ok to replace the existing263 * security manager.264 * This may result in throwing a SecurityException.265 *266 *

    Otherwise, the argument is established as the current267 * security manager. If the argument is null and no268 * security manager has been established, then no action is taken

    and269 * the method simply returns.270 *

    271 * @param s the security manager.272 * @exception SecurityException if the security manager has

    already273 * been set and its checkPermission

    method274 * doesn't allow it to be replaced.275 * @see #getSecurityManager276 * @see SecurityManager#checkPermission277 * @see java.lang.RuntimePermission278 */279 public static280 void setSecurityManager(final SecurityManager s) {281 try {282 s.checkPackageAccess("java.lang");283 } catch (Exception e) {284 // no-op285 }286 setSecurityManager0(s);287 }288289 private static synchronized290 void setSecurityManager0(final SecurityManager s) {291 SecurityManager sm = getSecurityManager();292 if (sm != null) {293 // ask the currently installed security manager if we294 // can replace it.295 sm.checkPermission(new RuntimePermission

    296 ("setSecurityManager"));297 }298299 if ((s != null) && (s.getClass().getClassLoader() != null)) {300 // New security manager class is not on bootstrap

    classpath.301 // Cause policy to get initialized before we install the

    new302 // security manager, in order to prevent infinite loops

    when

  • 8/4/2019 System Class of Java

    7/25

    303 // trying to initialize the policy (which usually involves304 // accessing some security and/or system properties,

    which in turn305 // calls the installed security manager's checkPermission

    method306 // which will loop infinitely if there is a non-system

    class307 // (in this case: the new security manager class) on the

    stack).308 AccessController.doPrivileged(new

    PrivilegedAction() {309 public Object run() {310 s.getClass().getProtectionDomain().implies311 (SecurityConstants.ALL_PERMISSION);312 return null;313 }314 });315 }316317 security = s;

    318 }319320 /**321 * Gets the system security interface.322 *323 * @return if a security manager has already been established

    for the324 * current application, then that security manager is

    returned;325 * otherwise, null is returned.326 * @see #setSecurityManager327 */328 public static SecurityManager getSecurityManager() {329 return security;330 }331332 /**333 * Returns the current time in milliseconds. Note that334 * while the unit of time of the return value is a millisecond,335 * the granularity of the value depends on the underlying336 * operating system and may be larger. For example, many337 * operating systems measure time in units of tens of338 * milliseconds.339 *340 *

    See the description of the class Date for341 * a discussion of slight discrepancies that may arise between342 * "computer time" and coordinated universal time (UTC).

    343 *344 * @return the difference, measured in milliseconds, between345 * the current time and midnight, January 1, 1970 UTC.346 * @see java.util.Date347 */348 public static native long currentTimeMillis();349350 /**351 * Returns the current value of the running Java Virtual Machine's352 * high-resolution time source, in nanoseconds.

  • 8/4/2019 System Class of Java

    8/25

    353 *354 *

    This method can only be used to measure elapsed time and is355 * not related to any other notion of system or wall-clock time.356 * The value returned represents nanoseconds since some fixed but357 * arbitrary origin time (perhaps in the future, so values358 * may be negative). The same origin is used by all invocations

    of359 * this method in an instance of a Java virtual machine; other360 * virtual machine instances are likely to use a different origin.361 *362 *

    This method provides nanosecond precision, but not

    necessarily363 * nanosecond resolution (that is, how frequently the value

    changes)364 * - no guarantees are made except that the resolution is at

    least as365 * good as that of {@link #currentTimeMillis()}.366 *367 *

    Differences in successive calls that span greater than368 * approximately 292 years (263 nanoseconds) will not

    369 * correctly compute elapsed time due to numerical overflow.370 *371 *

    The values returned by this method become meaningful only

    when372 * the difference between two such values, obtained within the

    same373 * instance of a Java virtual machine, is computed.374 *375 *

    For example, to measure how long some code takes to

    execute:376 * {@code377 * long startTime = System.nanoTime();378 * // ... the code being measured ...379 * long estimatedTime = System.nanoTime() - startTime;}380 *381 *

    To compare two nanoTime values382 * {@code383 * long t0 = System.nanoTime();384 * ...385 * long t1 = System.nanoTime();}386 *387 * one should use {@code t1 - t0 < 0}, not {@code t1 < t0},388 * because of the possibility of numerical overflow.389 *390 * @return the current value of the running Java Virtual Machine's391 * high-resolution time source, in nanoseconds392 * @since 1.5

    393 */394 public static native long nanoTime();395396 /**397 * Copies an array from the specified source array, beginning at

    the398 * specified position, to the specified position of the

    destination array.399 * A subsequence of array components are copied from the source400 * array referenced by src to the destination array

  • 8/4/2019 System Class of Java

    9/25

    401 * referenced by dest. The number of componentscopied is402 * equal to the length argument. The components at403 * positions srcPos through404 * srcPos+length-1 in the source array are copied

    into405 * positions destPos through406 * destPos+length-1, respectively, of the destination407 * array.408 *

    409 * If the src and dest arguments refer

    to the410 * same array object, then the copying is performed as if the411 * components at positions srcPos through412 * srcPos+length-1 were first copied to a temporary413 * array with length components and then the

    contents of414 * the temporary array were copied into positions415 * destPos through destPos+length-1 of

    the

    416 * destination array.417 *

    418 * If dest is null, then a419 * NullPointerException is thrown.420 *

    421 * If src is null, then a422 * NullPointerException is thrown and the destination423 * array is not modified.424 *

    425 * Otherwise, if any of the following is true, an426 * ArrayStoreException is thrown and the destination

    is427 * not modified:428 * 429 * The src argument refers to an object that is

    not an430 * array.431 * The dest argument refers to an object that is

    not an432 * array.433 * The src argument and dest

    argument refer434 * to arrays whose component types are different primitive

    types.435 * The src argument refers to an array with a

    primitive436 * component type and the dest argument refers to

    an array437 * with a reference component type.438 * The src argument refers to an array with a

    reference439 * component type and the dest argument refers to

    an array440 * with a primitive component type.441 * 442 *

    443 * Otherwise, if any of the following is true, an

  • 8/4/2019 System Class of Java

    10/25

    444 * IndexOutOfBoundsException is445 * thrown and the destination is not modified:446 * 447 * The srcPos argument is negative.448 * The destPos argument is negative.449 * The length argument is negative.450 * srcPos+length is greater than451 * src.length, the length of the source array.452 * destPos+length is greater than453 * dest.length, the length of the destination

    array.454 * 455 *

    456 * Otherwise, if any actual component of the source array from457 * position srcPos through458 * srcPos+length-1 cannot be converted to the

    component459 * type of the destination array by assignment conversion, an460 * ArrayStoreException is thrown. In this case, let461 * k be the smallest nonnegative integer less than

    462 * length such that src[srcPos+k]463 * cannot be converted to the component type of the destination464 * array; when the exception is thrown, source array components

    from465 * positions srcPos through466 * srcPos+k-1467 * will already have been copied to destination array positions468 * destPos through469 * destPos+k-1 and no other470 * positions of the destination array will have been modified.471 * (Because of the restrictions already itemized, this472 * paragraph effectively applies only to the situation where both473 * arrays have component types that are reference types.)474 *475 * @param src the source array.476 * @param srcPos starting position in the source array.477 * @param dest the destination array.478 * @param destPos starting position in the destination data.479 * @param length the number of array elements to be copied.480 * @exception IndexOutOfBoundsException if copying would cause481 * access of data outside array bounds.482 * @exception ArrayStoreException if an element in the

    src483 * array could not be stored into the

    dest array484 * because of a type mismatch.485 * @exception NullPointerException if either src or

    486 * dest is null.487 */488 public static native void arraycopy(Object src, int srcPos,489 Object dest, int destPos,490 int length);491492 /**493 * Returns the same hash code for the given object as494 * would be returned by the default method hashCode(),495 * whether or not the given object's class overrides

  • 8/4/2019 System Class of Java

    11/25

    496 * hashCode().497 * The hash code for the null reference is zero.498 *499 * @param x object for which the hashCode is to be calculated500 * @return the hashCode501 * @since JDK1.1502 */503 public static native int identityHashCode(Object x);504505 /**506 * System properties. The following properties are guaranteed to

    be defined:507 * 508 * java.version Java version number509 * java.vendor Java vendor specific string510 * java.vendor.url Java vendor URL511 * java.home Java installation directory512 * java.class.version Java class version number513 * java.class.path Java classpath514 * os.name Operating System Name

    515 * os.arch Operating System Architecture516 * os.version Operating System Version517 * file.separator File separator ("/" on Unix)518 * path.separator Path separator (":" on Unix)519 * line.separator Line separator ("\n" on Unix)520 * user.name User account name521 * user.home User home directory522 * user.dir User's current working directory523 * 524 */525526 private static Properties props;527 private static native Properties initProperties(Properties props);528529 /**530 * Determines the current system properties.531 *

    532 * First, if there is a security manager, its533 * checkPropertiesAccess method is called with no534 * arguments. This may result in a security exception.535 *

    536 * The current set of system properties for use by the537 * {@link #getProperty(String)} method is returned as a538 * Properties object. If there is no current set of539 * system properties, a set of system properties is first created

    and540 * initialized. This set of system properties always includes

    values541 * for the following keys:542 * 543 * Key544 * Description of Associated Value545 * java.version546 * Java Runtime Environment version547 * java.vendor548 * Java Runtime Environment vendor

  • 8/4/2019 System Class of Java

    12/25

    550 * Java vendor URL551 * java.home552 * Java installation directory553 * java.vm.specification.version554 * Java Virtual Machine specification version555 * java.vm.specification.vendor556 * Java Virtual Machine specification vendor557 * java.vm.specification.name558 * Java Virtual Machine specification name559 * java.vm.version560 * Java Virtual Machine implementation version561 * java.vm.vendor562 * Java Virtual Machine implementation vendor563 * java.vm.name564 * Java Virtual Machine implementation name565 * java.specification.version566 * Java Runtime Environment specification

    version567 * java.specification.vendor568 * Java Runtime Environment specification

    vendor569 * java.specification.name570 * Java Runtime Environment specification name571 * java.class.version572 * Java class format version number573 * java.class.path574 * Java class path575 * java.library.path576 * List of paths to search when loading

    libraries577 * java.io.tmpdir578 * Default temp file path579 * java.compiler580 * Name of JIT compiler to use581 * java.ext.dirs582 * Path of extension directory or directories583 * os.name584 * Operating system name585 * os.arch586 * Operating system architecture587 * os.version588 * Operating system version589 * file.separator590 * File separator ("/" on UNIX)591 * path.separator592 * Path separator (":" on UNIX)593 * line.separator

    594 * Line separator ("\n" on UNIX)595 * user.name596 * User's account name597 * user.home598 * User's home directory599 * user.dir600 * User's current working directory601 * 602 *

  • 8/4/2019 System Class of Java

    13/25

    603 * Multiple paths in a system property value are separated by thepath604 * separator character of the platform.605 *

    606 * Note that even if the security manager does not permit the607 * getProperties operation, it may choose to permit

    the608 * {@link #getProperty(String)} operation.609 *610 * @return the system properties611 * @exception SecurityException if a security manager exists

    and its612 * checkPropertiesAccess method doesn't

    allow access613 * to the system properties.614 * @see #setProperties615 * @see java.lang.SecurityException616 * @see java.lang.SecurityManager#checkPropertiesAccess()617 * @see java.util.Properties618 */

    619 public static Properties getProperties() {620 SecurityManager sm = getSecurityManager();621 if (sm != null) {622 sm.checkPropertiesAccess();623 }624625 return props;626 }627628 /**629 * Returns the system-dependent line separator string. It always630 * returns the same value - the initial value of the {@linkplain631 * #getProperty(String) system property} {@code line.separator}.632 *633 *

    On UNIX systems, it returns {@code "\n"}; on Microsoft634 * Windows systems it returns {@code "\r\n"}.635 */636 public static String lineSeparator() {637 return lineSeparator;638 }639640 private static String lineSeparator;641642 /**643 * Sets the system properties to the Properties644 * argument.645 *

    646 * First, if there is a security manager, its647 * checkPropertiesAccess method is called with no648 * arguments. This may result in a security exception.649 *

    650 * The argument becomes the current set of system properties for

    use651 * by the {@link #getProperty(String)} method. If the argument is652 * null, then the current set of system properties is653 * forgotten.654 *

  • 8/4/2019 System Class of Java

    14/25

    655 * @param props the new system properties.656 * @exception SecurityException if a security manager exists

    and its657 * checkPropertiesAccess method doesn't

    allow access658 * to the system properties.659 * @see #getProperties660 * @see java.util.Properties661 * @see java.lang.SecurityException662 * @see java.lang.SecurityManager#checkPropertiesAccess()663 */664 public static void setProperties(Properties props) {665 SecurityManager sm = getSecurityManager();666 if (sm != null) {667 sm.checkPropertiesAccess();668 }669 if (props == null) {670 props = new Properties();671 initProperties(props);672 }

    673 System.props = props;674 }675676 /**677 * Gets the system property indicated by the specified key.678 *

    679 * First, if there is a security manager, its680 * checkPropertyAccess method is called with the key

    as681 * its argument. This may result in a SecurityException.682 *

    683 * If there is no current set of system properties, a set of

    system684 * properties is first created and initialized in the same manner

    as685 * for the getProperties method.686 *687 * @param key the name of the system property.688 * @return the string value of the system property,689 * or null if there is no property with

    that key.690 *691 * @exception SecurityException if a security manager exists

    and its692 * checkPropertyAccess method doesn't

    allow693 * access to the specified system property.

    694 * @exception NullPointerException if key is695 * null.696 * @exception IllegalArgumentException if key is

    empty.697 * @see #setProperty698 * @see java.lang.SecurityException699 * @see

    java.lang.SecurityManager#checkPropertyAccess(java.lang.String)700 * @see java.lang.System#getProperties()701 */

  • 8/4/2019 System Class of Java

    15/25

    702 public static String getProperty(String key) {703 checkKey(key);704 SecurityManager sm = getSecurityManager();705 if (sm != null) {706 sm.checkPropertyAccess(key);707 }708709 return props.getProperty(key);710 }711712 /**713 * Gets the system property indicated by the specified key.714 *

    715 * First, if there is a security manager, its716 * checkPropertyAccess method is called with the717 * key as its argument.718 *

    719 * If there is no current set of system properties, a set of

    system720 * properties is first created and initialized in the same manner

    as721 * for the getProperties method.722 *723 * @param key the name of the system property.724 * @param def a default value.725 * @return the string value of the system property,726 * or the default value if there is no property with

    that key.727 *728 * @exception SecurityException if a security manager exists

    and its729 * checkPropertyAccess method doesn't

    allow730 * access to the specified system property.731 * @exception NullPointerException if key is732 * null.733 * @exception IllegalArgumentException if key is

    empty.734 * @see #setProperty735 * @see

    java.lang.SecurityManager#checkPropertyAccess(java.lang.String)736 * @see java.lang.System#getProperties()737 */738 public static String getProperty(String key, String def) {739 checkKey(key);740 SecurityManager sm = getSecurityManager();741 if (sm != null) {

    742 sm.checkPropertyAccess(key);743 }744745 return props.getProperty(key, def);746 }747748 /**749 * Sets the system property indicated by the specified key.750 *

    751 * First, if a security manager exists, its

  • 8/4/2019 System Class of Java

    16/25

    752 * SecurityManager.checkPermission method753 * is called with a PropertyPermission(key, "write")754 * permission. This may result in a SecurityException being

    thrown.755 * If no exception is thrown, the specified property is set to

    the given756 * value.757 *

    758 *759 * @param key the name of the system property.760 * @param value the value of the system property.761 * @return the previous value of the system property,762 * or null if it did not have one.763 *764 * @exception SecurityException if a security manager exists

    and its765 * checkPermission method doesn't allow766 * setting of the specified property.767 * @exception NullPointerException if key or768 * value is null.

    769 * @exception IllegalArgumentException if key isempty.770 * @see #getProperty771 * @see java.lang.System#getProperty(java.lang.String)772 * @see java.lang.System#getProperty(java.lang.String,

    java.lang.String)773 * @see java.util.PropertyPermission774 * @see SecurityManager#checkPermission775 * @since 1.2776 */777 public static String setProperty(String key, String value) {778 checkKey(key);779 SecurityManager sm = getSecurityManager();780 if (sm != null) {781 sm.checkPermission(new PropertyPermission(key,782 SecurityConstants.PROPERTY_WRITE_ACTION));783 }784785 return (String) props.setProperty(key, value);786 }787788 /**789 * Removes the system property indicated by the specified key.790 *

    791 * First, if a security manager exists, its792 * SecurityManager.checkPermission method793 * is called with a PropertyPermission(key, "write")

    794 * permission. This may result in a SecurityException beingthrown.795 * If no exception is thrown, the specified property is removed.796 *

    797 *798 * @param key the name of the system property to be

    removed.799 * @return the previous string value of the system property,800 * or null if there was no property with

    that key.

  • 8/4/2019 System Class of Java

    17/25

    801 *802 * @exception SecurityException if a security manager exists

    and its803 * checkPropertyAccess method doesn't

    allow804 * access to the specified system property.805 * @exception NullPointerException if key is806 * null.807 * @exception IllegalArgumentException if key is

    empty.808 * @see #getProperty809 * @see #setProperty810 * @see java.util.Properties811 * @see java.lang.SecurityException812 * @see java.lang.SecurityManager#checkPropertiesAccess()813 * @since 1.5814 */815 public static String clearProperty(String key) {816 checkKey(key);817 SecurityManager sm = getSecurityManager();

    818 if (sm != null) {819 sm.checkPermission(new PropertyPermission(key, "write"));820 }821822 return (String) props.remove(key);823 }824825 private static void checkKey(String key) {826 if (key == null) {827 throw new NullPointerException("key can't be null");828 }829 if (key.equals("")) {830 throw new IllegalArgumentException("key can't be empty");831 }832 }833834 /**835 * Gets the value of the specified environment variable. An836 * environment variable is a system-dependent external named837 * value.838 *839 *

    If a security manager exists, its840 * {@link SecurityManager#checkPermission checkPermission}841 * method is called with a842 * {@link RuntimePermission}("getenv."+name)843 * permission. This may result in a {@link SecurityException}844 * being thrown. If no exception is thrown the value of the

    845 * variable name is returned.846 *847 *

    System848 * properties and environment variables are both849 * conceptually mappings between names and values. Both850 * mechanisms can be used to pass user-defined information to a851 * Java process. Environment variables have a more global effect,852 * because they are visible to all descendants of the process853 * which defines them, not just the immediate Java subprocess.854 * They can have subtly different semantics, such as case

  • 8/4/2019 System Class of Java

    18/25

    855 * insensitivity, on different operating systems. For these856 * reasons, environment variables are more likely to have857 * unintended side effects. It is best to use system properties858 * where possible. Environment variables should be used when a859 * global effect is desired, or when an external system interface860 * requires an environment variable (such as PATH).861 *862 *

    On UNIX systems the alphabetic case of name is863 * typically significant, while on Microsoft Windows systems it is864 * typically not. For example, the expression865 * System.getenv("FOO").equals(System.getenv("foo"))866 * is likely to be true on Microsoft Windows.867 *868 * @param name the name of the environment variable869 * @return the string value of the variable, or null870 * if the variable is not defined in the system

    environment871 * @throws NullPointerException if name is

    null872 * @throws SecurityException

    873 * if a security manager exists and its874 * {@link SecurityManager#checkPermission checkPermission}875 * method doesn't allow access to the environment variable876 * name877 * @see #getenv()878 * @see ProcessBuilder#environment()879 */880 public static String getenv(String name) {881 SecurityManager sm = getSecurityManager();882 if (sm != null) {883 sm.checkPermission(new RuntimePermission("getenv."+name));884 }885886 return ProcessEnvironment.getenv(name);887 }888889890 /**891 * Returns an unmodifiable string map view of the current system

    environment.892 * The environment is a system-dependent mapping from names to893 * values which is passed from parent to child processes.894 *895 *

    If the system does not support environment variables, an896 * empty map is returned.897 *898 *

    The returned map will never contain null keys or values.

    899 * Attempting to query the presence of a null key or value will900 * throw a {@link NullPointerException}. Attempting to query901 * the presence of a key or value which is not of type902 * {@link String} will throw a {@link ClassCastException}.903 *904 *

    The returned map and its collection views may not obey the905 * general contract of the {@link Object#equals} and906 * {@link Object#hashCode} methods.907 *

  • 8/4/2019 System Class of Java

    19/25

    908 *

    The returned map is typically case-sensitive on allplatforms.909 *910 *

    If a security manager exists, its911 * {@link SecurityManager#checkPermission checkPermission}912 * method is called with a913 * {@link RuntimePermission}("getenv.*")914 * permission. This may result in a {@link SecurityException}

    being915 * thrown.916 *917 *

    When passing information to a Java subprocess,918 * system properties919 * are generally preferred over environment variables.920 *921 * @return the environment as a map of variable names to values922 * @throws SecurityException923 * if a security manager exists and its924 * {@link SecurityManager#checkPermission checkPermission}925 * method doesn't allow access to the process environment

    926 * @see #getenv(String)927 * @see ProcessBuilder#environment()928 * @since 1.5929 */930 public static java.util.Map getenv() {931 SecurityManager sm = getSecurityManager();932 if (sm != null) {933 sm.checkPermission(new RuntimePermission("getenv.*"));934 }935936 return ProcessEnvironment.getenv();937 }938939 /**940 * Terminates the currently running Java Virtual Machine. The941 * argument serves as a status code; by convention, a nonzero

    status942 * code indicates abnormal termination.943 *

    944 * This method calls the exit method in class945 * Runtime. This method never returns normally.946 *

    947 * The call System.exit(n) is effectively equivalent

    to948 * the call:949 * 950 * Runtime.getRuntime().exit(n)

    951 * 952 *953 * @param status exit status.954 * @throws SecurityException955 * if a security manager exists and its

    checkExit956 * method doesn't allow exit with the specified status.957 * @see java.lang.Runtime#exit(int)958 */959 public static void exit(int status) {

  • 8/4/2019 System Class of Java

    20/25

    960 Runtime.getRuntime().exit(status);961 }962963 /**964 * Runs the garbage collector.965 *

    966 * Calling the gc method suggests that the Java

    Virtual967 * Machine expend effort toward recycling unused objects in order

    to968 * make the memory they currently occupy available for quick

    reuse.969 * When control returns from the method call, the Java Virtual970 * Machine has made a best effort to reclaim space from all

    discarded971 * objects.972 *

    973 * The call System.gc() is effectively equivalent to

    the974 * call:

    975 * 976 * Runtime.getRuntime().gc()977 * 978 *979 * @see java.lang.Runtime#gc()980 */981 public static void gc() {982 Runtime.getRuntime().gc();983 }984985 /**986 * Runs the finalization methods of any objects pending

    finalization.987 *

    988 * Calling this method suggests that the Java Virtual Machine

    expend989 * effort toward running the finalize methods of

    objects990 * that have been found to be discarded but whose

    finalize991 * methods have not yet been run. When control returns from the992 * method call, the Java Virtual Machine has made a best effort to993 * complete all outstanding finalizations.994 *

    995 * The call System.runFinalization() is effectively996 * equivalent to the call:997 *

    998 * Runtime.getRuntime().runFinalization()999 * 1000 *1001 * @see java.lang.Runtime#runFinalization()1002 */1003 public static void runFinalization() {1004 Runtime.getRuntime().runFinalization();1005 }10061007 /**

  • 8/4/2019 System Class of Java

    21/25

    1008 * Enable or disable finalization on exit; doing so specifiesthat the1009 * finalizers of all objects that have finalizers that have notyet been1010 * automatically invoked are to be run before the Java runtimeexits.1011 * By default, finalization on exit is disabled.1012 *1013 *

    If there is a security manager,1014 * its checkExit method is first called1015 * with 0 as its argument to ensure the exit is allowed.1016 * This could result in a SecurityException.1017 *1018 * @deprecated This method is inherently unsafe. It may resultin1019 * finalizers being called on live objects while otherthreads are1020 * concurrently manipulating those objects, resulting inerratic1021 * behavior or deadlock.

    1022 * @param value indicating enabling or disabling of finalization1023 * @throws SecurityException1024 * if a security manager exists and itscheckExit1025 * method doesn't allow the exit.1026 *1027 * @see java.lang.Runtime#exit(int)1028 * @see java.lang.Runtime#gc()1029 * @see java.lang.SecurityManager#checkExit(int)1030 * @since JDK1.11031 */1032 @Deprecated1033 public static void runFinalizersOnExit(boolean value) {1034 Runtime.getRuntime().runFinalizersOnExit(value);1035 }10361037 /**1038 * Loads a code file with the specified filename from the localfile1039 * system as a dynamic library. The filename1040 * argument must be a complete path name.1041 *

    1042 * The call System.load(name) is effectivelyequivalent1043 * to the call:1044 * 1045 * Runtime.getRuntime().load(name)

    1046 * 1047 *1048 * @param filename the file to load.1049 * @exception SecurityException if a security manager existsand its1050 * checkLink method doesn't allow1051 * loading of the specified dynamic library1052 * @exception UnsatisfiedLinkError if the file does not exist.1053 * @exception NullPointerException if filename is1054 * null

  • 8/4/2019 System Class of Java

    22/25

    1055 * @see java.lang.Runtime#load(java.lang.String)1056 * @seejava.lang.SecurityManager#checkLink(java.lang.String)1057 */1058 public static void load(String filename) {1059 Runtime.getRuntime().load0(getCallerClass(), filename);1060 }10611062 /**1063 * Loads the system library specified by the libname1064 * argument. The manner in which a library name is mapped to the1065 * actual system library is system dependent.1066 *

    1067 * The call System.loadLibrary(name) is effectively1068 * equivalent to the call1069 * 1070 * Runtime.getRuntime().loadLibrary(name)1071 * 1072 *1073 * @param libname the name of the library.

    1074 * @exception SecurityException if a security manager existsand its1075 * checkLink method doesn't allow1076 * loading of the specified dynamic library1077 * @exception UnsatisfiedLinkError if the library does notexist.1078 * @exception NullPointerException if libname is1079 * null1080 * @see java.lang.Runtime#loadLibrary(java.lang.String)1081 * @seejava.lang.SecurityManager#checkLink(java.lang.String)1082 */1083 public static void loadLibrary(String libname) {1084 Runtime.getRuntime().loadLibrary0(getCallerClass(), libname);1085 }10861087 /**1088 * Maps a library name into a platform-specific stringrepresenting1089 * a native library.1090 *1091 * @param libname the name of the library.1092 * @return a platform-dependent native library name.1093 * @exception NullPointerException if libname is1094 * null1095 * @see java.lang.System#loadLibrary(java.lang.String)1096 * @see java.lang.ClassLoader#findLibrary(java.lang.String)

    1097 * @since 1.21098 */1099 public static native String mapLibraryName(String libname);11001101 /**1102 * Initialize the system class. Called after threadinitialization.1103 */1104 private static void initializeSystemClass() {1105

  • 8/4/2019 System Class of Java

    23/25

    1106 // VM might invoke JNU_NewStringPlatform() to set thoseencoding1107 // sensitive properties (user.home, user.name,boot.class.path, etc.)1108 // during "props" initialization, in which it may needaccess, via1109 // System.getProperty(), to the related system encodingproperty that1110 // have been initialized (put into "props") at early stage ofthe1111 // initialization. So make sure the "props" is available atthe1112 // very beginning of the initialization and all systemproperties to1113 // be put into it directly.1114 props = new Properties();1115 initProperties(props); // initialized by the VM11161117 // There are certain system configurations that may becontrolled by

    1118 // VM options such as the maximum amount of direct memory and1119 // Integer cache size used to support the object identitysemantics1120 // of autoboxing. Typically, the library will obtain thesevalues1121 // from the properties set by the VM. If the properties arefor1122 // internal implementation use only, these properties shouldbe1123 // removed from the system properties.1124 //1125 // See java.lang.Integer.IntegerCache and the1126 // sun.misc.VM.saveAndRemoveProperties method for example.1127 //1128 // Save a private copy of the system properties object that1129 // can only be accessed by the internal implementation.Remove1130 // certain system properties that are not intended for publicaccess.1131 sun.misc.VM.saveAndRemoveProperties(props);113211331134 lineSeparator = props.getProperty("line.separator");1135 sun.misc.Version.init();11361137 FileInputStream fdIn = new FileInputStream(FileDescriptor.in);1138 FileOutputStream fdOut = new

    FileOutputStream(FileDescriptor.out);1139 FileOutputStream fdErr = newFileOutputStream(FileDescriptor.err);1140 setIn0(new BufferedInputStream(fdIn));1141 setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128),true));1142 setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128),true));1143 // Load the zip library now in order to keepjava.util.zip.ZipFile

  • 8/4/2019 System Class of Java

    24/25

    1144 // from trying to use itself to load this library later.1145 loadLibrary("zip");11461147 // Setup Java signal handlers for HUP, TERM, and INT (whereavailable).1148 Terminator.setup();11491150 // Initialize any miscellenous operating system settings thatneed to be1151 // set for the class libraries. Currently this is no-opeverywhere except1152 // for Windows where the process-wide error mode is setbefore the java.io1153 // classes are used.1154 sun.misc.VM.initializeOSEnvironment();11551156 // Subsystems that are invoked during initialization caninvoke1157 // sun.misc.VM.isBooted() in order to avoid doing things thatshould

    1158 // wait until the application class loader has been set up.1159 sun.misc.VM.booted();11601161 // The main thread is not added to its thread group in thesame1162 // way as other threads; we must do it ourselves here.1163 Thread current = Thread.currentThread();1164 current.getThreadGroup().add(current);11651166 // register shared secrets1167 setJavaLangAccess();1168 }11691170 private static void setJavaLangAccess() {1171 // Allow privileged classes outside of java.lang1172 sun.misc.SharedSecrets.setJavaLangAccess(newsun.misc.JavaLangAccess(){1173 public sun.reflect.ConstantPool getConstantPool(Classklass) {1174 return klass.getConstantPool();1175 }1176 public void setAnnotationType(Class klass, AnnotationTypetype) {1177 klass.setAnnotationType(type);1178 }1179 public AnnotationType getAnnotationType(Class klass) {1180 return klass.getAnnotationType();

    1181 }1182 public 1183 E[] getEnumConstantsShared(Class klass) {1184 return klass.getEnumConstantsShared();1185 }1186 public void blockedOn(Thread t, Interruptible b) {1187 t.blockedOn(b);1188 }1189 public void registerShutdownHook(int slot, booleanregisterShutdownInProgress, Runnable hook) {

  • 8/4/2019 System Class of Java

    25/25

    1190 Shutdown.add(slot, registerShutdownInProgress, hook);1191 }1192 public int getStackTraceDepth(Throwable t) {1193 return t.getStackTraceDepth();1194 }1195 public StackTraceElement getStackTraceElement(Throwablet, int i) {1196 return t.getStackTraceElement(i);1197 }1198 });1199 }12001201 /* returns the class of the caller. */1202 static Class getCallerClass() {1203 // NOTE use of more generic Reflection.getCallerClass()1204 return Reflection.getCallerClass(3);1205 }1206 }

    Save This Page

    Home openjdk-7 java lang [javadoc | source]

    http://www.docjar.com/http://www.docjar.com/projects/openjdk-7-java.htmlhttp://www.docjar.com/projects/openjdk-7-java.htmlhttp://www.docjar.com/docs/api/java/lang/package-index.htmlhttp://www.docjar.com/docs/api/java/lang/System.htmlhttp://del.icio.us/posthttp://www.docjar.com/http://www.docjar.com/projects/openjdk-7-java.htmlhttp://www.docjar.com/docs/api/java/lang/package-index.htmlhttp://www.docjar.com/docs/api/java/lang/System.html