package Java_Test; public class System1 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("java.vendor")); System.out.println(System.getProperty("java.home")); System.out.println(System.getProperty("java.library.path")); System.out.println(System.getProperty("user.dir")); //獲取當前類工做目錄 System.out.println(System.getProperty("os.version")); System.out.println(System.getProperty("os.name")); System.out.println(System.getenv("path")); } }
package com.test; import java.util.Properties; public class MyTest { public static void main(String[] args) { Properties properties = System.getProperties(); for (Object key : properties.keySet()) { System.out.println(key + " : " + properties.getProperty((String) key)); } } }
源碼查看:全部的屬性都存在System的一個內部屬性Properties中,Properties是一個繼承HashTable的類java
(1)System.getPropertiesgit
public static Properties getProperties() { SecurityManager sm = getSecurityManager(); if (sm != null) { sm.checkPropertiesAccess(); } return props; }
private static Properties props;
(2)Properties是一個繼承HashTable的類web
public class Properties extends Hashtable<Object,Object> { private static final long serialVersionUID = 4112578634029874840L; protected Properties defaults; 。。。 }
結果:apache
java.runtime.name : Java(TM) SE Runtime Environment
sun.boot.library.path : C:\Program Files\Java\jdk1.7.0_80\jre\bin
java.vm.version : 24.80-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.country : CN
user.script :
sun.java.launcher : SUN_STANDARD
sun.os.patch.level :
java.vm.specification.name : Java Virtual Machine Specification
user.dir : E:\xiangmu\Mytest
java.runtime.version : 1.7.0_80-b15
java.awt.graphicsenv : sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs : C:\Program Files\Java\jdk1.7.0_80\jre\lib\endorsed
os.arch : amd64
java.io.tmpdir : C:\Users\ADMINI~1\AppData\Local\Temp\
line.separator :
java.vm.specification.vendor : Oracle Corporation
user.variant :
os.name : Windows 8.1
sun.jnu.encoding : GBK
java.library.path : C:\Program Files\Java\jdk1.7.0_80\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.7.0_80/bin/../jre/bin/server;C:/Program Files/Java/jdk1.7.0_80/bin/../jre/bin;C:/Program Files/Java/jdk1.7.0_80/bin/../jre/lib/amd64;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;E:\soft\apache-maven-3.5.3\bin;C:\WINDOWS\System32\OpenSSH\;E:\git\Git\cmd;E:\SVN\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;E:\soft\maven\apache-maven-3.3.9\bin;C:\Program Files\MySQL\MySQL Server 5.7\bin;C:\Program Files\Java\jdk1.7.0_80\bin;E:\git\Git\bin;E:\git\Git\usr\bin;E:\git\Git;C:\Program Files\Java\jdk1.7.0_80\jre\bin;D:\zdcontomcat\zdc8\lo\program;;E:\soft\eclipse\eclipse-jee-mars-1-win32-x86_64\eclipse;;.
java.specification.name : Java Platform API Specification
java.class.version : 51.0
sun.management.compiler : HotSpot 64-Bit Tiered Compilers
os.version : 6.3
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 : E:\xiangmu\Mytest\bin;E:\xiangmu\Mytest\WebContent\WEB-INF\lib\log4j-1.2.16.jar;E:\xiangmu\Mytest\WebContent\WEB-INF\lib\slf4j-api-1.5.10.jar;E:\xiangmu\Mytest\WebContent\WEB-INF\lib\slf4j-log4j12-1.5.10.jar;E:\tomcat\apache-tomcat-7.0.88\lib\annotations-api.jar;E:\tomcat\apache-tomcat-7.0.88\lib\catalina-ant.jar;E:\tomcat\apache-tomcat-7.0.88\lib\catalina-ha.jar;E:\tomcat\apache-tomcat-7.0.88\lib\catalina-tribes.jar;E:\tomcat\apache-tomcat-7.0.88\lib\catalina.jar;E:\tomcat\apache-tomcat-7.0.88\lib\ecj-4.4.2.jar;E:\tomcat\apache-tomcat-7.0.88\lib\el-api.jar;E:\tomcat\apache-tomcat-7.0.88\lib\jasper-el.jar;E:\tomcat\apache-tomcat-7.0.88\lib\jasper.jar;E:\tomcat\apache-tomcat-7.0.88\lib\jsp-api.jar;E:\tomcat\apache-tomcat-7.0.88\lib\servlet-api.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-api.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-coyote.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-dbcp.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-i18n-es.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-i18n-fr.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-i18n-ja.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-jdbc.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat-util.jar;E:\tomcat\apache-tomcat-7.0.88\lib\tomcat7-websocket.jar;E:\tomcat\apache-tomcat-7.0.88\lib\websocket-api.jar
user.name : Administrator
java.vm.specification.version : 1.7
sun.java.command : com.test.MyTest
java.home : C:\Program Files\Java\jdk1.7.0_80\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_80
java.ext.dirs : C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
sun.boot.class.path : C:\Program Files\Java\jdk1.7.0_80\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_80\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
public static void main(String[] args) { System.setProperty("mykey", "myvalue"); String property = System.getProperty("mykey"); System.out.println(property); try { Thread.sleep(100 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } }
咱們能夠在cmd窗口經過jinfo查看變量windows
C:\Users\Administrator>jps 1236 8544 MyTest 9384 Jps C:\Users\Administrator>jinfo 8544 Attaching to process ID 8544, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.80-b11 Java System Properties: java.runtime.name = Java(TM) SE Runtime Environment java.vm.version = 24.80-b11 sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_80\jre\bin java.vendor.url = http://java.oracle.com/ java.vm.vendor = Oracle Corporation path.separator = ; file.encoding.pkg = sun.io java.vm.name = Java HotSpot(TM) 64-Bit Server VM sun.os.patch.level = sun.java.launcher = SUN_STANDARD user.script = user.country = CN user.dir = E:\xiangmu\Mytest java.vm.specification.name = Java Virtual Machine Specification java.runtime.version = 1.7.0_80-b15 mykey = myvalue
// 讀取單個環境變量 String str = System.getenv("JAVA_HOME"); System.out.println(str); // 讀取全部的環境變量 Map<String, String> getenv = System.getenv(); for (Map.Entry<String, String> entry : getenv.entrySet()) { System.out.println(entry.getKey() + "========" + entry.getValue()); }
結果:api
C:\Program Files\Java\jdk1.7.0_80 USERPROFILE========C:\Users\Administrator ProgramData========C:\ProgramData PATHEXT========.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC Classpath========.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar JAVA_HOME========C:\Program Files\Java\jdk1.7.0_80 MAVEN_HOME========E:\soft\maven\apache-maven-3.3.9
.......
java.versiontomcat |
Java 運行時環境版本websocket |
java.vendor架構 |
Java 運行時環境供應商oracle |
java.vendor.url |
Java 供應商的 URL |
java.home |
Java 安裝目錄 |
java.vm.specification.version |
Java 虛擬機規範版本 |
java.vm.specification.vendor |
Java 虛擬機規範供應商 |
java.vm.specification.name |
Java 虛擬機規範名稱 |
java.vm.version |
Java 虛擬機實現版本 |
java.vm.vendor |
Java 虛擬機實現供應商 |
java.vm.name |
Java 虛擬機實現名稱 |
java.specification.version |
Java 運行時環境規範版本 |
java.specification.vendor |
Java 運行時環境規範供應商 |
java.specification.name |
Java 運行時環境規範名稱 |
java.class.version |
Java 類格式版本號 |
java.class.path |
Java 類路徑 |
java.library.path |
加載庫時搜索的路徑列表 |
java.io.tmpdir |
默認的臨時文件路徑 |
java.compiler |
要使用的 JIT 編譯器的名稱 |
java.ext.dirs |
一個或多個擴展目錄的路徑 |
os.name |
操做系統的名稱 |
os.arch |
操做系統的架構 |
os.version |
操做系統的版本 |
file.separator |
文件分隔符(在 UNIX 系統中是「/」) |
path.separator |
路徑分隔符(在 UNIX 系統中是「:」) |
line.separator |
行分隔符(在 UNIX 系統中是「/n」) |
user.name |
用戶的帳戶名稱 |
user.home |
用戶的主目錄 |
user.dir |
用戶的當前工做目錄 |