如何在Java中获取系统属性?

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

Java中的System类维护一组属性。
这些属性以键/值对的形式存储。
键和值都是定义当前工作环境的特征或者属性的字符串。

您可以使用两种方法来读取系统属性:getProperty()和getProperties()。

用Java获取所有系统属性

System.getProperties()返回所有系统属性的枚举。
以下代码在控制台上打印所有系统属性。

import java.util.Enumeration;
import java.util.Properties;

public class Main {

  public static void main(String[] args)
  {
      Properties p = System.getProperties();
      Enumeration keys = p.keys();
      while (keys.hasMoreElements()) {
          String key = (String)keys.nextElement();
          String value = (String)p.get(key);
          System.out.println(key + ": " + value);
      }
  }
}

输出

文本形式的输出:

gopherProxySet: false
awt.toolkit: sun.lwawt.macosx.LWCToolkit
java.specification.version: 11
sun.cpu.isalist: 
sun.jnu.encoding: UTF-8
java.class.path: /Users/jayant/Desktop/java/JD1/out/production/JD1
java.vm.vendor: Oracle Corporation
sun.arch.data.model: 64
java.vendor.url: http://java.oracle.com/
user.timezone: 
os.name: Mac OS X
java.vm.specification.version: 11
sun.java.launcher: SUN_STANDARD
user.country: GB
sun.boot.library.path: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib
sun.java.command: Main
http.nonProxyHosts: local|*.local|169.254/16|*.169.254/16
jdk.debug: release
sun.cpu.endian: little
user.home: /Users/jayant
user.language: en
java.specification.vendor: Oracle Corporation
java.version.date: 2016-10-16
java.home: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
file.separator: /
java.vm.compressedOopsMode: Zero based
line.separator: 

java.specification.name: Java Platform API Specification
java.vm.specification.vendor: Oracle Corporation
java.awt.graphicsenv: sun.awt.CGraphicsEnvironment
sun.management.compiler: HotSpot 64-Bit Tiered Compilers
ftp.nonProxyHosts: local|*.local|169.254/16|*.169.254/16
java.runtime.version: 11.0.2+7-LTS
user.name: jayant
path.separator: :
os.version: 10.14.2
java.runtime.name: Java(TM) SE Runtime Environment
file.encoding: UTF-8
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
java.vendor.version: 18.9
java.vendor.url.bug: http://bugreport.java.com/bugreport/
java.io.tmpdir: /var/folders/56/fc29wjz520x_21fmrl9r2jgc0000gn/T/
java.version: 11.0.2
user.dir: /Users/jayant/Desktop/java/JD1
os.arch: x86_64
java.vm.specification.name: Java Virtual Machine Specification
java.awt.printerjob: sun.lwawt.macosx.CPrinterJob
sun.os.patch.level: unknown
java.library.path: /Users/jayant/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.vendor: Oracle Corporation
java.vm.info: mixed mode
java.vm.version: 11.0.2+7-LTS
sun.io.unicode.encoding: UnicodeBig
java.class.version: 55.0
socksNonProxyHosts: local|*.local|169.254/16|*.169.254/16

Process finished with exit code 0

重要系统属性

一些重要的系统属性是:

"file.separator";File separator (for example, "/";)
"java.class.path";Java classpath
"java.class.version";Java class version number
"java.home";Java installation directory
"java.vendor";Java vendor-specific string
"java.vendor.url";Java vendor URL
"java.version";Java version number
"line.separator";Line separator
"os.arch";Operating system architecture
"os.name";Operating system name
"os.version";Operating system version
"path.separator";Path separator (for example, ":";)
"user.language";Language used by User
"user.dir";User's current working directory
"user.home";User home directory
"user.name";User account name

获取特定的系统属性

要从列表中获取特定属性,请使用System.Property(key)。
其中key是要检索的属性的名称。
输出以字符串形式返回。
如果属性键不匹配,则返回null。

public class Main {

  public static void main(String[] args)
  {
      System.out.println(System.getProperty("java.class.path"));
      System.out.println(System.getProperty("os.name"));
      System.out.println(System.getProperty("user.name"));
  }
}

输出

/Users/jayant/Desktop/java/JD1/out/production/JD1
Mac OS X
jayant

这三个属性已被打印出来。

还有另一种变体,可以让您指定在属性名称不匹配的情况下必须打印的内容。
请注意以下内容在第四行和第五行中的区别:

public class Main {

  public static void main(String[] args)
  {
System.out.println(System.getProperty("java.class.path"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("user.name"));
System.out.println(System.getProperty("hello"));
System.out.println(System.getProperty("hello","property not found"));
  }
}

输出

/Users/jayant/Desktop/java/JD1/out/production/JD1
Mac OS X
jayant
null
property not found

第四行返回null,因为" hello"与任何属性名称都不匹配。
第五行返回我们在代码"找不到属性"中提到的行。