1. 程式人生 > 實用技巧 >java中System.getProperties()與System.getEnv()

java中System.getProperties()與System.getEnv()

其實在執行虛擬機器時,經常一些虛擬機器引數或者系統引數

getEnv()方法

System.getEnv()可以獲取到我們在作業系統中的設定的一些環境變數

public static void testGetEnv() {
        Map<String, String> env = System.getenv();
        for(String key:env.keySet()) {
            System.out.println(key+"="+env.get(key));
        }
    }

結果執行

USERDOMAIN_ROAMINGPROFILE=LAPTOP-L8MRCMCM
LOCALAPPDATA
=C:\Users\hua'wei\AppData\Local PROCESSOR_LEVEL=6 USERDOMAIN=LAPTOP-L8MRCMCM FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer LOGONSERVER=\\LAPTOP-L8MRCMCM JAVA_HOME=D:\program\jdk-15 SESSIONNAME=Console ALLUSERSPROFILE=C:\ProgramData PROCESSOR_ARCHITECTURE=AMD64 PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0
\Modules SystemDrive=C: MAVEN_HOME=D:\program\apache-maven-3.6.3 OneDrive=C:\Users\hua'wei\OneDrive APPDATA=C:\Users\hua'wei\AppData\Roaming USERNAME=hua'wei ProgramFiles(x86)=C:\Program Files (x86) CommonProgramFiles=C:\Program Files\Common Files Path=D:/program/sts-4.8.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932/jre/bin/server;D:/program/sts-4.8.0.RELEASE
//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932/jre/bin;D:\program\vmware_workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\program\jdk-15\bin;D:\program\jdk-15\jre\bin;D:\program\apache-maven-3.6.3\bin;D:\program\mysql-8.0.21-winx64\bin;C:\Users\hua'wei\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Bandizip\;D:\program\sts-4.8.0.RELEASE;
FPS_BROWSER_USER_PROFILE_STRING=Default PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC DriverData=C:\Windows\System32\Drivers\DriverData OS=Windows_NT classpath=.;D:\program\jdk-15\lib;D:\program\jdk-15\lib\tools.jar COMPUTERNAME=LAPTOP-L8MRCMCM

上述的輸出結果有的是系統自帶的環境變數,有的是我們自己設定的系統變數.

getProperties()方法

getProperties()方法可以獲取到java虛擬機器方面的引數

    public static void testGetProperties() {
        Properties properties = System.getProperties();
        for(Object key:properties.keySet()) {
            Object object = properties.get(key);
            System.out.println(key+"="+object);
        }
    }

輸出結果

java.specification.version=15
sun.cpu.isalist=amd64
sun.jnu.encoding=GBK
java.class.path=D:\workspace\algorithm-pro\target\classes
java.vm.vendor=Oracle Corporation
sun.arch.data.model=64
user.variant=
java.vendor.url=https://java.oracle.com/
java.vm.specification.version=15
os.name=Windows 10
sun.java.launcher=SUN_STANDARD
user.country=CN
sun.boot.library.path=D:\program\jdk-15\bin
sun.java.command=com.resource.location.SystemDemo
jdk.debug=release
sun.cpu.endian=little
user.home=C:\Users\hua'wei
user.language=zh
java.specification.vendor=Oracle Corporation
java.version.date=2020-09-15
java.home=D:\program\jdk-15
file.separator=\
java.vm.compressedOopsMode=Zero based
line.separator=

java.vm.specification.vendor=Oracle Corporation
java.specification.name=Java Platform API Specification
user.script=
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
java.runtime.version=15+36-1562
user.name=hua'wei
path.separator=;
os.version=10.0
java.runtime.name=Java(TM) SE Runtime Environment
file.encoding=UTF-8
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug=https://bugreport.java.com/bugreport/
java.io.tmpdir=C:\Users\hua'wei\AppData\Local\Temp\
java.version=15
user.dir=D:\workspace\algorithm-pro
os.arch=amd64
java.vm.specification.name=Java Virtual Machine Specification
sun.os.patch.level=
java.library.path=D:\program\jdk-15\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/program/sts-4.8.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932/jre/bin/server;D:/program/sts-4.8.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_14.0.2.v20200815-0932/jre/bin;D:\program\vmware_workstation\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\program\jdk-15\bin;D:\program\jdk-15\jre\bin;D:\program\apache-maven-3.6.3\bin;D:\program\mysql-8.0.21-winx64\bin;C:\Users\hua'wei\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Bandizip\;D:\program\sts-4.8.0.RELEASE;;.
java.vm.info=mixed mode, sharing
java.vendor=Oracle Corporation
java.vm.version=15+36-1562
sun.io.unicode.encoding=UnicodeLittle
java.class.version=59.0