Java Gets system variables (environment variables and set variables)

Source: Internet
Author: User

Preface

The concept of environmental variables is not unfamiliar, is the operating system environment variables.

System variables are variables maintained by Java itself. obtained by System.getproperty Way.

For different operating systems, there may be some disunity in the processing of environment variables, such as: no distinction between uppercase and lowercase, and so on.


Java Get environment variables

Java gets environment variables in a very easy way:

System.getenv () Get all the environment variables

System.getenv (key) Gets the value of an environment variable

Map map = System.getenv (); Iterator it = Map.entryset (). Iterator (); while (It.hasnext ()) {Entry Entry = (Entry) it.next (); System.out.print (Entry.getkey () + "="); System.out.println (Entry.getvalue ());}
Assuming it is a Windows system, the printed values are the same as the environment variables seen from "My Computer".


Java getting and setting system variables

The way Java gets environment variables is also very easy:

System.getproperties () Get all the system variables

System.getproperty (key) Gets the value of a system variable

Properties Properties = System.getproperties (); Iterator it =  properties.entryset (). Iterator (); while (It.hasnext ( ) {Entry Entry = (Entry) it.next (); System.out.print (Entry.getkey () + "="); System.out.println (Entry.getvalue ());}

System variables, in addition to being available, can be set by System.setproperty (key, value) to the system variables they need.


By default, Java sets which system variables:

Java.version Java Execution Time environment version number
Java.vendor Java Execution Time environment provider
Java.vendor.url Java Vendor's URL
Java.home Java installation Folder
Java.vm.specification.version Java Virtual Machine Specification version number
Java.vm.specification.vendor Java Virtual Machine specification Provider
Java.vm.specification.name Java Virtual Machine Specification Name
Java.vm.version Java Virtual Machine Implementation version number
Java.vm.vendor Java virtual Machine implementation Provider
Java.vm.name Java virtual Machine implementation name
Java.specification.version Java Execution Time Environment specification version number
Java.specification.vendor Java Execution Time Environment specification vendor
Java.specification.name Java Execution Time 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 into storage
Java.io.tmpdir Default Temporary file path
Java.compiler the name of the JIT compiler to use
Java.ext.dirs the path of one or more extended folders
Os.name name of the operating system
Architecture of the Os.arch operating system
Os.version version number of the operating system
File.separator file Delimiter ("/" in UNIX system)
Path.separator path Delimiter (":" In UNIX system)
Line.separator line Delimiter ("/n" in UNIX systems)
User.Name User's account name
User.home the user's home folder
User.dir the user's current working folder




Supplement

1.  In. bat; The. cmd or. SH Sets some variables in the form of set.

For example, WebLogic's Setdomainenv.cmd

Set sun_java_home=c:\oracle\middleware\jdk160_21

Environment variables are set here.

2. In log4j configuration, the generation path of log file is sometimes configured.

For example ${log_dir}/logfile.log, the log_dir here is replaced by a variable of the system attribute.

3. Take a look at the Java source code, the way to get the system variable by system.getproperties (), there is a safe check

    public static Properties GetProperties () {SecurityManager sm = getSecurityManager ();        if (SM! = null) {    sm.checkpropertiesaccess ();} return props;    }

In a single Java application test, the SecurityManager in the System is empty.

When the applet executes, the. policy file is combined to check permissions.


Suppose that an empty securitymanager will be found to throw a permission exception.

public static void Main (string[] args) {//TODO auto-generated method Stubsystem.setsecuritymanager (New SecurityManager ( );//securitymanager sm = System.getsecuritymanager ();//system.out.println (SM); System.getsecuritymanager (). checkpropertiesaccess ();}


Java Gets system variables (environment variables and set variables)

Related Article

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.