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

The System class in Java maintains a set of properties. These properties are stored in the form of key/value pairs. Both keys and values are Strings that define traits or attributes of the current working environment.

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

There are two methods that you can use to read the system properties: getProperty() and getProperties().

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

用Java获取所有系统属性 (Getting All System Properties in Java )

System.getProperties() returns an Enumeration of all the system properties. The following code prints all the system properties on the console.

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);
        }
    }
}
System properties
Output
输出量

The Output in text form:

文本形式的输出:


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: 2018-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

重要系统属性 (Important System Properties)

Some of the important system properties are:

一些重要的系统属性是:

“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
“ file.separator” 文件分隔符(例如,“ /”)
“ java.class.path” Java类路径
“ java.class.version” Java类版本号
“ java.home” Java安装目录
“ java.vendor” Java供应商特定的字符串
“ java.vendor.url” Java供应商URL
“ java.version” Java版本号
“ line.separator” 行分隔符
“ os.arch” 操作系统架构
“ os.name” 操作系统名称
“ os.version” 作业系统版本
“ path.separator” 路径分隔符(例如,“:”)
“ user.language” 用户使用的语言
“ user.dir” 用户的当前工作目录
“ user.home” 用户主目录
“用户名” 用户帐号名称

获取特定的系统属性 (Getting a Specific System Property )

To get a particular property from the list use System.Property(key). Where key is the name of the property you want to retrieve. The output is returned as a string. If the property key doesn’t match then null is returned.

要从列表中获取特定属性,请使用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"));
    }
}
Output Properties
Output
输出量

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

The three properties have been printed out.

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

There is another variation that lets you specify what has to be printed in case the property name doesn’t match. Observe the difference in the fourth and fifth line in the following :

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


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"));
    }
}
printing properties
Output
输出量

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

The fourth line returns null, since “hello” doesn’t match with any property name. The fifth line returns the line we mentioned in the code “property not found”.

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

结论 (Conclusion )

We can retrieve system properties using the above-mentioned methods. Information such as the version of Java in use, home directory, name of Java vendor can be retrieved from the System properties.

我们可以使用上述方法检索系统属性。 可以从“系统”属性中检索诸如正在使用的Java版本,主目录,Java供应商名称之类的信息。

翻译自: https://www.journaldev.com/41760/get-system-properties-in-java

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值