Java System.getProperty()

时间:2020-02-23 14:36:56  来源:igfitidea点击:

Java System.getProperty方法对于查找操作系统信息非常有用。

Java System.getProperty

我们可以使用System.getProperty()方法来查找操作系统信息,用户主目录,Java运行时版本,路径分隔符,行分隔符,用户工作目录,Java主目录,Java类路径以及各种其他有用的信息。

Java System getProperty示例

Java系统类的getProperties()方法返回属性的键集,这里我们将使用它列出所有系统属性。

package com.theitroad.util;

import java.util.Properties;
import java.util.Set;

public class JavaSystemGetProperty {

	public static void main(String[] args) {
		Properties prop = System.getProperties();
		Set<Object> keySet = prop.keySet();
		for (Object obj : keySet) {
			System.out.println("System Property: {" 
					+ obj.toString() + "," 
					+ System.getProperty(obj.toString()) + "}");
		}
	}

}

上面的System getProperty示例程序的输出是;

System Property: {java.runtime.name,Java(TM) SE Runtime Environment}
System Property: {sun.boot.library.path,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib}
System Property: {java.vm.version,25.131-b11}
System Property: {user.country.format,IN}
System Property: {gopherProxySet,false}
System Property: {java.vm.vendor,Oracle Corporation}
System Property: {java.vendor.url,https://java.oracle.com/}
System Property: {path.separator,:}
System Property: {java.vm.name,Java HotSpot(TM) 64-Bit Server VM}
System Property: {file.encoding.pkg,sun.io}
System Property: {user.country,US}
System Property: {sun.java.launcher,SUN_STANDARD}
System Property: {sun.os.patch.level,unknown}
System Property: {java.vm.specification.name,Java Virtual Machine Specification}
System Property: {user.dir,/Users/hyman/Documents/eclipse-workspace/JavaExceptions}
System Property: {java.runtime.version,1.8.0_131-b11}
System Property: {java.awt.graphicsenv,sun.awt.CGraphicsEnvironment}
System Property: {java.endorsed.dirs,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/endorsed}
System Property: {os.arch,x86_64}
System Property: {java.io.tmpdir,/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/}
System Property: {line.separator,
}
System Property: {java.vm.specification.vendor,Oracle Corporation}
System Property: {os.name,Mac OS X}
System Property: {sun.jnu.encoding,UTF-8}
System Property: {java.library.path,/Users/hyman/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.}
System Property: {java.specification.name,Java Platform API Specification}
System Property: {java.class.version,52.0}
System Property: {sun.management.compiler,HotSpot 64-Bit Tiered Compilers}
System Property: {os.version,10.12.6}
System Property: {http.nonProxyHosts,local|*.local|169.254/16|*.169.254/16}
System Property: {user.home,/Users/hyman}
System Property: {user.timezone,}
System Property: {java.awt.printerjob,sun.lwawt.macosx.CPrinterJob}
System Property: {file.encoding,UTF-8}
System Property: {java.specification.version,1.8}
System Property: {java.class.path,/Users/hyman/Documents/eclipse-workspace/JavaExceptions/bin:/Users/hyman/temp/commons-io-2.5.jar}
System Property: {user.name,hyman}
System Property: {java.vm.specification.version,1.8}
System Property: {sun.java.command,com.theitroad.util.JavaSystemGetProperty}
System Property: {java.home,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre}
System Property: {sun.arch.data.model,64}
System Property: {user.language,en}
System Property: {java.specification.vendor,Oracle Corporation}
System Property: {awt.toolkit,sun.lwawt.macosx.LWCToolkit}
System Property: {java.vm.info,mixed mode}
System Property: {java.version,1.8.0_131}
System Property: {java.ext.dirs,/Users/hyman/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java}
System Property: {sun.boot.class.path,/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/classes}
System Property: {java.vendor,Oracle Corporation}
System Property: {file.separator,/}
System Property: {java.vendor.url.bug,https://bugreport.sun.com/bugreport/}
System Property: {sun.io.unicode.encoding,UnicodeBig}
System Property: {sun.cpu.endian,little}
System Property: {socksNonProxyHosts,local|*.local|169.254/16|*.169.254/16}
System Property: {ftp.nonProxyHosts,local|*.local|169.254/16|*.169.254/16}
System Property: {sun.cpu.isalist,}

上面印刷的一些有价值的信息是:

  • 该程序在Mac OS上运行,请参见输出{os.name,Mac OS X}
  • Java版本为1.8.0_131,请参见系统属性输出{java.version,1.8.0_131}
  • 用户名是hyman,如输出{user.name,hyman}中所示
  • 路径分隔符是冒号(:),请参见输出{path.separator,:}
  • 它是64位操作系统,请参见输出{os.arch,x86_64}

还有很多其他有用的信息,您应该检查一下。

我们应该在需要确保程序独立于平台的地方使用此系统信息,例如,创建文件路径时应使用file.separator属性,并在读取PATH信息时应使用path.separator变量。