Java System.getProperty()

Java System.getProperty method is very useful to find out the operating system information.

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

Java System.getProperty (Java System.getProperty)

We can use System.getProperty() method to find out the Operating System information, user home directory, Java runtime version, path separator, line separator, user working directory, Java home, Java classpath, and various other useful information.

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

Java System getProperty示例 (Java System getProperty Example)

Java System class getProperties() method returns the Set of keys for the properties and here we will use it to list out all the system properties.

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

package com.journaldev.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()) + "}");
		}
	}

}

The output of the above System getProperty example program is;

上面的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/pankaj/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/pankaj/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/pankaj}
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/pankaj/Documents/eclipse-workspace/JavaExceptions/bin:/Users/pankaj/temp/commons-io-2.5.jar}
System Property: {user.name,pankaj}
System Property: {java.vm.specification.version,1.8}
System Property: {sun.java.command,com.journaldev.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/pankaj/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,}

Some of the valuable information being printed above are;

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

  1. The program is running on Mac OS, see output {os.name,Mac OS X}

    该程序在Mac OS上运行,请参见输出{os.name,Mac OS X}
  2. Java version is 1.8.0_131, see system property output {java.version,1.8.0_131}

    Java版本是1.8.0_131 ,请参见系统属性输出{java.version,1.8.0_131}
  3. User name is pankaj, as seen in output {user.name,pankaj}

    用户名是pankaj,如输出{user.name,pankaj}
  4. Path separator is colon (:), see output {path.separator,:}

    路径分隔符是冒号(:),请参见输出{path.separator,:}
  5. It’s a 64-bit OS, see output {os.arch,x86_64}

    这是一个64位操作系统,请参见输出{os.arch,x86_64}

There are many more useful information printed too, you should check them out.

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

We should use this System information where we need to make sure our program is platform independent, for example creating the file path we should use file.separator property and to read PATH information we should use path.separator variable. That’s all about System.getProperty in java.

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

翻译自: https://www.journaldev.com/904/java-system-getproperty

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值