【README】
本文主要介绍java运行环境变量的获取,如何读取 env.properties 文件并将自定义变量写入到系统变量;
【1】System.getenv() 获取环境变量
public static void main1() {
Map<String, String> envMap = System.getenv();
envMap.entrySet().forEach(x-> System.out.println(x.getKey() + "=" + x.getValue()));
}
System.getenv() 获取的是操作系统环境变量列表,如下:
【2】 System.getProperties()获取java运行时环境变量
public static void main2() {
Properties props = System.getProperties();
props.list(System.out);
}
-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=D:\Java\jdk1.8.0_172\jre\bin
java.vm.version=25.172-b11
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.script=
user.country=CN
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
java.vm.specification.name=Java Virtual Machine Specification
user.dir=D:\bench-cluster\spring_in_action_ecl...
java.runtime.version=1.8.0_172-b11
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D:\Java\jdk1.8.0_172\jre\lib\endorsed
os.arch=amd64
java.io.tmpdir=C:\Users\zhangsan\AppData\Local\Temp\
line.separator=
java.vm.specification.vendor=Oracle Corporation
user.variant=
os.name=Windows 10
sun.jnu.encoding=GBK
java.library.path=D:\Java\jdk1.8.0_172\bin;C:\WINDOWS\S...
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=10.0
user.home=C:\Users\zhangsan
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=UTF-8
java.specification.version=1.8
user.name=zhangsan
java.class.path=D:\Java\jdk1.8.0_172\jre\lib\charsets...
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=D:\Java\jdk1.8.0_172\jre
sun.java.command=com.swjtu.mybatis.main.Main2
java.specification.vendor=Oracle Corporation
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0_172
java.ext.dirs=D:\Java\jdk1.8.0_172\jre\lib\ext;C:\W...
sun.boot.class.path=D:\Java\jdk1.8.0_172\jre\lib\resource...
java.vendor=Oracle Corporation
file.separator=\
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=amd64zheshi
【3】自定义变量
1)新建 env.properties 文件, 填写变量
2)读取 自定义变量文件,env.properties 文件;
// 自定义变量
public static void main3() {
Properties envProps = new Properties();
try {
// 使用 mybatis Resources工具读取环境属性文件
envProps.load(Resources.getResourceAsStream("env.properties"));
} catch (IOException e) {
}
// 把自定义变量填充到系统变量 SystemProperties
envProps.stringPropertyNames().forEach(x->{
System.getProperties().setProperty(x, envProps.getProperty(x));
});
System.out.println(System.getenv());
System.getProperties().list(System.out);
}
【4】几个常用系统变量
Properties sysProps = System.getProperties();
System.out.println("user.dir=" + sysProps.getProperty("user.dir"));
System.out.println("java.class.path=" + sysProps.getProperty("java.class.path"));
System.out.println("sun.java.command=" + sysProps.getProperty("sun.java.command"));
System.out.println("file.separator=" + sysProps.getProperty("file.separator"));
System.out.println("file.encoding=" + sysProps.getProperty("file.encoding"));
user.dir=D:\bench-cluster\spring_in_action_eclipse\MybatisHello3_mapper
java.class.path=D:\Java\jdk1.8.0_172\jre\lib\charsets.jar;D:\Java\jdk1.8.0_172\jre\lib\deploy.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_172\jre\lib\javaws.jar;D:\Java\jdk1.8.0_172\jre\lib\jce.jar;D:\Java\jdk1.8.0_172\jre\lib\jfr.jar;D:\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_172\jre\lib\jsse.jar;D:\Java\jdk1.8.0_172\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_172\jre\lib\plugin.jar;D:\Java\jdk1.8.0_172\jre\lib\resources.jar;D:\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\bench-cluster\spring_in_action_eclipse\MybatisHello3_mapper\target\classes;D:\software_cluster\mvn_repo\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;D:\software_cluster\mvn_repo\.m2\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar;D:\software_cluster\mvn_repo\.m2\repository\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar;D:\software_cluster\mvn_repo\.m2\repository\org\apache\logging\log4j\log4j-core\2.11.0\log4j-core-2.11.0.jar;D:\software_cluster\mvn_repo\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.0\log4j-api-2.11.0.jar;D:\Program Files\IntelliJ IDEA 2021.1\lib\idea_rt.jar
sun.java.command=com.swjtu.mybatis.main.Main2
file.separator=\
file.encoding=UTF-8
1)常用系统变量列表
- user.dir ,当前项目根目录;
- java.class.path ,java运行时环境路径;
- sum.java.command , 主程序所在类的全限定类名;
- file.separator, 文件路径分隔符;
- file.encoding,文件编码;
其中 file.encoding 是通过jvm运行时参数指定的, vm 参数如下:
D:\Java\jdk1.8.0_172\bin\java.exe "-javaagent:D:\Program Files\IntelliJ IDEA 2021.1\lib\idea_rt.jar=49223:D:\Program Files\IntelliJ IDEA 2021.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0_172\jre\lib\charsets.jar;D:\Java\jdk1.8.0_172\jre\lib\deploy.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_172\jre\lib\javaws.jar;D:\Java\jdk1.8.0_172\jre\lib\jce.jar;D:\Java\jdk1.8.0_172\jre\lib\jfr.jar;D:\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_172\jre\lib\jsse.jar;D:\Java\jdk1.8.0_172\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_172\jre\lib\plugin.jar;D:\Java\jdk1.8.0_172\jre\lib\resources.jar;D:\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\bench-cluster\spring_in_action_eclipse\MybatisHello3_mapper\target\classes;D:\software_cluster\mvn_repo\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;D:\software_cluster\mvn_repo\.m2\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar;D:\software_cluster\mvn_repo\.m2\repository\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar;D:\software_cluster\mvn_repo\.m2\repository\org\apache\logging\log4j\log4j-core\2.11.0\log4j-core-2.11.0.jar;D:\software_cluster\mvn_repo\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.0\log4j-api-2.11.0.jar com.swjtu.mybatis.main.Main2
即通过 -Dfile.encoding=UTF-8 指定的jvm参数,可以通过 System.getProperties() 获取到;
|