有時候咱們項目須要獲取一些參數,若是都使用配置文件的話,每一個應用都要在項目中添加配置文件,比較麻煩。因此java提供了兩個方法能夠獲取參數。java
這兩個方法就是System.getProperties和System.getenv.apache
1、System.getPropertieswindows
這個方法獲取的是java相關的參數,包括經過-Dkey=value指定的參數。api
2、System.getenvoracle
這個方法獲取的是系統相關的參數,包括在環境變量上設置的相關參數。maven
3、使用apiui
public static void main(String[] args) { Properties ps = System.getProperties(); Set<Entry<Object, Object>> set = ps.entrySet(); for (Entry<Object, Object> s : set) { System.out.println(s); } System.out.println(System.getProperty("java.version")); Map<String, String> map = System.getenv(); System.out.println(map); System.out.println(System.getenv("CLASS_PATH")); }
輸出結果:google
java.runtime.name=Java(TM) SE Runtime Environment sun.boot.library.path=D:\Program Files\Java\jdk1.7.0_79\jre\bin java.vm.version=24.79-b02 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.country=CN user.script= sun.java.launcher=SUN_STANDARD sun.os.patch.level=Service Pack 1 java.vm.specification.name=Java Virtual Machine Specification user.dir=D:\work\jar-test java.runtime.version=1.7.0_79-b15 java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment java.endorsed.dirs=D:\Program Files\Java\jdk1.7.0_79\jre\lib\endorsed os.arch=amd64 java.io.tmpdir=C:\Users\liubin\AppData\Local\Temp\ line.separator= java.vm.specification.vendor=Oracle Corporation user.variant= os.name=Windows 7 sun.jnu.encoding=GBK java.library.path=D:\Program Files\Java\jdk1.7.0_79\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\TortoiseSVN\bin;D:\Program Files\Java\jdk1.7.0_79\bin;D:\tools\apache-maven-3.2.5\bin;D:\tools\zookeeper-3.5.0-alpha\bin;. java.specification.name=Java Platform API Specification java.class.version=51.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=6.1 user.home=C:\Users\Administrator user.timezone= java.awt.printerjob=sun.awt.windows.WPrinterJob file.encoding=UTF-8 java.specification.version=1.7 java.class.path=D:\work\jar-test\target\classes;C:\Users\Administrator\.m2\repository\com\googlecode\aviator\aviator\2.3.3\aviator-2.3.3.jar;C:\Users\Administrator\.m2\repository\commons-beanutils\commons-beanutils\1.9.2\commons-beanutils-1.9.2.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;C:\Users\Administrator\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar user.name=liubin java.vm.specification.version=1.7 sun.java.command=com.lubby.aviator.MyTest java.home=D:\Program Files\Java\jdk1.7.0_79\jre sun.arch.data.model=64 user.language=zh java.specification.vendor=Oracle Corporation awt.toolkit=sun.awt.windows.WToolkit java.vm.info=mixed mode java.version=1.7.0_79 java.ext.dirs=D:\Program Files\Java\jdk1.7.0_79\jre\lib\ext;C:\Windows\Sun\Java\lib\ext sun.boot.class.path=D:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;D:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;D:\Program Files\Java\jdk1.7.0_79\jre\lib\sunrsasign.jar;D:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;D:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.7.0_79\jre\classes java.vendor=Oracle Corporation file.separator=\ java.vendor.url.bug=http://bugreport.sun.com/bugreport/ sun.io.unicode.encoding=UnicodeLittle sun.cpu.endian=little sun.desktop=windows sun.cpu.isalist=amd64 1.7.0_79 {USERPROFILE=C:\Users\liubin, ProgramData=C:\ProgramData, USERDNSDOMAIN=CORP.DHF100.COM, PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, windows_tracing_logfile=C:\BVTBin\Tests\installpackage\csilogfile.log, JAVA_HOME=D:\Program Files\Java\jdk1.7.0_79, ProgramFiles(x86)=C:\Program Files (x86), CLASS_PATH=D:\Program Files\Java\jdk1.7.0_79\lib\dt.jar;D:\Program Files\Java\jdk1.7.0_79\lib\tools.jar;, windows_tracing_flags=3, TEMP=C:\Users\liubin\AppData\Local\Temp, SystemDrive=C:, ProgramFiles=C:\Program Files, Path=C:\Program Files (x86)\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\TortoiseSVN\bin;D:\Program Files\Java\jdk1.7.0_79\bin;D:\tools\apache-maven-3.2.5\bin;D:\tools\zookeeper-3.5.0-alpha\bin, HOMEDRIVE=C:, PROCESSOR_REVISION=3c03, USERDOMAIN=CORP, ALLUSERSPROFILE=C:\ProgramData, ProgramW6432=C:\Program Files, DEVMGR_SHOW_DETAILS=1, PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 60 Stepping 3, GenuineIntel, SESSIONNAME=Console, TMP=C:\Users\liubin\AppData\Local\Temp, CommonProgramFiles=C:\Program Files\Common Files, =::=::\, LOGONSERVER=\\DHF-SHS-001, M2_HOME=D:\tools\apache-maven-3.2.5, PROCESSOR_ARCHITECTURE=AMD64, FP_NO_HOST_CHECK=NO, OS=Windows_NT, HOMEPATH=\Users\liubin, PROCESSOR_LEVEL=6, CommonProgramW6432=C:\Program Files\Common Files, LOCALAPPDATA=C:\Users\liubin\AppData\Local, COMPUTERNAME=LIUBIN-PC, windir=C:\Windows, SystemRoot=C:\Windows, NUMBER_OF_PROCESSORS=8, USERNAME=liubin, PUBLIC=C:\Users\Public, PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\, CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files, ComSpec=C:\Windows\system32\cmd.exe, APPDATA=C:\Users\liubin\AppData\Roaming} D:\Program Files\Java\jdk1.7.0_79\lib\dt.jar;D:\Program Files\Java\jdk1.7.0_79\lib\tools.jar;