How to set system parameters and operating parameters in Java

System parameters

System-level global variables, this parameter can be accessed anywhere in the program. It has the highest priority and overrides the configuration of the same name in the program.

The standard format of system parameters is: -Dargname=argvalue, multiple parameters are separated by spaces. If there is a space in the parameter value, it is enclosed in quotation marks.

Among them, the parameter name can be the Java default. Such parameters are automatically recognized and effective by the JVM virtual machine. For example, -Dfile.encoding=UTF-8 is used to specify the file encoding format; it can also be user-defined, for example,- Dmy=user, the parameter value can be read in the program, and the related logic can be executed.

For the parameter key-value pairs set in the virtual machine system parameters, you can use System.getProperty("propertyName") to get the corresponding parameter value in the program.

public static void main(String[] args) {
  String result = System.getProperty("argname");
  System.out.println("argname: " + result);
}

Operating parameters

The parameter values ​​passed in when the main method is executed, if there are multiple parameters, separate them with spaces.

The general format of the main method is: public static void main(String[] args), where Stringp[] args is a variable that stores operating parameters, which can be used directly in the program.

public static void main(String[] args) {
  if (args.length > 0) {
    for (int i = 0; i < args.length; i++) {
    	System.out.println("第" + i + "个参数为: " + args[i]);
    }
  }
}

Parameter settings in the command line

The basic format of the java command is java [-options] class [args...], where:

[-options] Configure Java system parameters

[args…] Configure Java operating parameters

示例:java -Dfile.encoding=UTF-8 -Dargname=argvalue Test hi a b c d

Parameter settings in IDEA

  • Run- Edit Configurations… - Spring Boot - App - Configuration - Environment
  • VM options: Set system parameters
  • Program arguments: set operating parameters

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113873231