Home Java javaTutorial java gets system variables (environment variables and setting variables)

java gets system variables (environment variables and setting variables)

Dec 17, 2016 pm 01:07 PM

Foreword

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

System variables are variables maintained by Java itself. Obtained through System.getProperty.

For different operating systems, there may be some inconsistencies in the processing of environment variables, such as: case insensitivity, etc.

Java gets environment variables

The way to get environment variables in Java is very simple:

System.getEnv() Gets all environment variables

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

[java] view plain copy
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());  
}
Copy after login

If it is a Windows system, the printed value is the same as the environment variable seen in "My Computer".


Java gets and sets system variables

Java’s way of getting environment variables is also very simple:

System.getProperties() Gets all system variables

System.getProperty(key) Gets a certain system variable In addition to obtaining the value of

[java] view plain copy
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());  
}
Copy after login


system variables, you can also set the system variables you need through System.setProperty(key, value).

What system variables are set by java by default:

java.version Java runtime environment version
java.vendor Java runtime environment vendor
java.vendor.url Java vendor URL
java.home Java installation Directory
java.vm.specification.version Java virtual machine specification version
java.vm.specification.vendor Java virtual machine specification supplier
java.vm.specification.name Java virtual machine specification name
java.vm.version Java virtual machine Implementation version
java.vm.vendor Java virtual machine implementation supplier
java.vm.name Java virtual machine implementation name
java.specification.version Java runtime environment specification version
java.specification.vendor Java runtime environment specification supplier
java.specification.name Java runtime environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path Path list searched when loading the library
java.io. tmpdir The default temporary file path
java.compiler The name of the JIT compiler to be used
java.ext.dirs The path to one or more extension directories
os.name The name of the operating system
os.arch The architecture of the operating system
os .version operating system version
file.separator file separator (in UNIX system is "/")
path.separator path separator (in UNIX system is ":")
line.separator line separator (in UNIX system In the system, it is "/n")
user.name The user's account name
user.home The user's home directory
user.dir The user's current working directory


Supplementary

1. In .bat; .cmd Or .sh will set some variables by set,

For example, weblogic's setDomainEnv.cmd

set SUN_JAVA_HOME=C:OracleMiddlewarejdk160_21

The environment variables are set here

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

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

3. Take a look at the java source code. When obtaining system variables through System.getProperties(), there will be a security check

[java] view plain copy
   public static Properties getProperties() {  
SecurityManager sm = getSecurityManager();  
       if (sm != null) {  
    sm.checkPropertiesAccess();  
}  
  
return props;  
   }
Copy after login


When testing a single Java application, the SecurityManager in System is empty.

When the Applet is running, it will check the permissions in combination with the .policy file.

If you give an empty SecurityManager, you will find that a permission exception will be thrown.

[java] view plain copy
public static void main(String[] args) {  
    // TODO Auto-generated method stub  
    System.setSecurityManager(new SecurityManager());  
    //SecurityManager sm = System.getSecurityManager();  
    //System.out.println(sm);  
    System.getSecurityManager().checkPropertiesAccess();  
}
Copy after login



For more java related articles on obtaining system variables (environment variables and setting variables), please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

In Java remote debugging, how to correctly obtain constant values ​​on remote servers? In Java remote debugging, how to correctly obtain constant values ​​on remote servers? Apr 19, 2025 pm 01:54 PM

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

See all articles