Java obtains system variables (environment variables and setting variables)

Source: Internet
Author: User
Preface

Environment variables are the environment variables of the operating system.

System variables are the variables maintained by Java. It is obtained through system. getproperty.

For different operating systems, the processing of environment variables may be case insensitive.

Java obtains Environment Variables

Java is easy to get environment variables:

System. getenv () to get all environment variables

System. getenv (key) to get 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());}

In Windows, the printed values are the same as the environment variables shown in "My Computer.

Java obtains and sets system variables.

Java is also easy to get environment variables:

System. getproperties () to get all System Variables

System. getproperty (key) to get 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());}

In addition to obtaining system variables, you can also set the system variables you need by using system. setproperty (Key, value.

Which system variables are set in Java by default:

Java. Version Java Runtime Environment version
Java. Vendor Java Runtime Environment vendor
Java. Vendor. url URL of the Java vendor
Java. Home Java installation directory
Java. VM. Specification. Version Java Virtual Machine standard version
Java. VM. Specification. Vendor Java virtual machine specification vendor
Java. VM. Specification. Name Java virtual machine specification name
Java. VM. Version Java Virtual Machine implementation version
Java. VM. Vendor Java Virtual Machine implementation vendor
Java. VM. Name Java Virtual Machine implementation name
Java. Specification. Version Java Runtime Environment Standard version
Java. Specification. Vendor Java Runtime Environment specification vendor
Java. Specification. Name Java Runtime Environment Standard name
Java. Class. Version Java class format version number
Java. Class. Path Java class path
List of paths searched when the java. Library. Path library is loaded
Java. Io. tmpdir default temporary file path
Name of the JIT compiler to be used by Java. Compiler
Java. Ext. dirs: path of one or more extension Directories
OS. Name of the Operating System
OS. Arch Operating System Architecture
OS. Version
File. Separator file delimiter ("/" in UNIX "/")
Path. Separator path separator (":" in UNIX systems ":")
Line. Separator line separator ("/n" in UNIX ")
User. Name: Account name of the user
User. Home user's home directory
Current working directory of user. dir

Supplement

1. Some variables will be set in. BAT;. CMD or. Sh through the set method,

For example, setdomainenv. CMD of Weblogic

Set sun_java_home = c: \ oracle \ middleware \ jdk160_21

Environment variables are set here.

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

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

3. Take a look at the Java source code. When you obtain system variables using the system. getproperties () method, a security check will be performed.

    public static Properties getProperties() {SecurityManager sm = getSecurityManager();        if (sm != null) {    sm.checkPropertiesAccess();}return props;    }

During a single Java application test, securitymanager in system is empty.

When the applet is running, the permission is checked in conjunction with the. Policy file.

If an empty securitymanager is assigned, a permission exception is thrown.

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();}

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.