java gets system variables (environment variables and setting variables)
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()); }
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()); }
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; }
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(); }
For more java related articles on obtaining system variables (environment variables and setting variables), please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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...

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...

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...

Start Spring using IntelliJIDEAUltimate version...

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. ...

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

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? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...
