Java gets system attributes and environment variables

Source: Internet
Author: User

When you need to use operating system-related variables (for example: file separators, newline characters) in your program, Java provides static methods for the System class getenv () and GetProperty () to return systems-related variables and properties, and the variables returned by the Getenv method are mostly system-dependent , the variables returned by the GetProperty method are mostly related to Java programs.

Both system properties and environment variables are mappings between names and values. Both mechanisms can be used to pass user-defined information to the Java process. Environment variables produce more global effects because they are visible not only to Java child processes, but to all child processes that define their processes. On different operating systems, there are subtle differences in semantics, such as case insensitive. Therefore, environment variables are more likely to have unintended side effects. Use System Properties whenever possible in your program. Environment variables should be used when global effects are required, or when external system interfaces require the use of environment variables (such as PATH).

1. Environment variable getenv (): Used to get the string map view of the current system environment; Test code and run results are as follows:

 publicclass Test {publicstaticvoid main (string[] args) {Map  <strin        G, string> map = system.getenv ();  for  (iterator<string> itr =            Map.keyset (). iterator (); Itr.hasnext ();) {String key  = Itr.next ();        System.out.println (key  + "=" + Map.get (key)); }       }} 
UserProfile        : User directory Userdnsdomain      : User domain pathext            : executable suffix java_home          : Java installation directory temp               : User temp file directory systemdrive        : system drive letter ProgramFiles       : Default program directory UserDomain         : Name of the domain of the account allusersprofile    : User Common directory SessionName        : Session name TMP                : Temp directory path               : PATH environment variable classpath          : CLASSPATH environment variable Processor_ ARCHITECTURE: Processor architecture OS                     : Operating system type Processor_level    : processing level computername       : computer name windir             : System installation directory systemroot         : System startup directory username           : User name ComSpec            : The exact path of the command line interpreter executable program AppData            : Application Data Directory

2. System attribute GetProperty (): The current system property. The test code and running results are as follows:

java.version Java: Runtime Environment version Java.vendor Java: Run-time environment vendor Java.vendor.url:Java Vendor Urljava.home&nbsp;&Nbsp;:java installation directory Java.vm.specification.version:Java Virtual machine specification version Java.vm.specification.vendor : Java Virtual Machine specification Vendor Java.vm.specification.name&nbsp: Java Virtual Machine Specification name Java.vm.version:Java virtual Machine Implementation version Java.vm.vendor:Java virtual machine implementation Vendor Java.vm.name&nbsp;  : Java Virtual machine Implementation name Java.specification.version:Java Runtime Environment Specification version Java.specification.vendor:Java Runtime Environment specification Vendor Java.specification.name : Java Runtime Environment Specification name Java.class. Version:java class format version number java.class. Path:java classpath Java.library.path: List of paths to search when loading libraries Java.io.tmpdir: Default temporary file path Java.compiler: The name of the JIT compiler to use Java.ext.dirs : path to one or more extended directories Os.name: Operating system name Os.arch: Operating system Schema os.version: OS version file.separator: File delimiter path.separator : Path delimiter line.separator: Line delimiter user.name: User's account name user.home: User's home directory user.dir: User's current working directory

Java gets system attributes and environment variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.