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);}}
}
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/16Process 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
输出量

/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"));}
}
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

如何在Java中获取系统属性?相关推荐

  1. 在Java中获取系统属性

    Java语言以其面向对象.跨平台.可移植性好.安全性高等优点,受到众多编程人员的青睐,越来越多的人将其作为应用软件开发语言的首选. 在Java应用程序运行时,特别是需要在跨平台工作环境下运行时,需要确 ...

  2. java文件中获取创建日期_如何在Java中获取文件的上次修改日期

    java文件中获取创建日期 Sometimes we need to get the file last modified date in Java, usually for listeners li ...

  3. 如何在Java中获取临时文件路径

    这是获取Java中临时文件路径的示例. 例 package com.mkyong.file;import java.io.File; import java.io.IOException;public ...

  4. java 获取文件扩展名_如何在Java中获取文件扩展名

    java 获取文件扩展名 Sometimes while working with files, we need to process them differently based on their ...

  5. 你如何在java中获取线程堆_如何在Windows上获取未在控制台中运行的Java进程的线程和堆转储...

    问题 我有一个Java应用程序,我从控制台运行,然后控制台执行另一个Java进程.我想获得该子进程的线程/堆转储. 在Unix上,我可以做akill -3 但是在Windows AFAIK上获取线程转 ...

  6. java获取语言_如何在java中获取语言环境对象?

    我在框架Spring和Liferay中使用Java. 使用liferay我知道如何获得一个语言环境(对象有一些信息:语言,国家......),但现在我在一个没有与liferay连接的java类,我不知 ...

  7. 如何在java中获取JVM的各种系统参数

    ManagementFactory ManagementFactory类是jdk1.5开始提供的一个工厂类,用于获取Java平台的受管Bean.该类提供了许多静态方法,每个方法返回一个或者多个jvm的 ...

  8. Java中获取系统日期时间/系统时间

    int y,m,d,h,mi,s;Calendar cal=Calendar.getInstance();y=cal.get(Calendar.YEAR);m=cal.get(Calendar.MON ...

  9. java窗口坐标_如何在Java中获取窗口外部的鼠标单击坐标

    尽管可能,但可能会受到限制: 为焦点事件添加一个AWTEventListener.只要您的应用在单击按钮之前就具有焦点,就会收到焦点丢失事件.然后查询指针位置. 限制是,当然,您的应用程序失去了焦点. ...

最新文章

  1. 10分钟了解Flutter跨平台运行原理!
  2. 在公司如何远程控制家中电脑
  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)...
  4. 使用docker镜像搭建svn+Apache环境
  5. C# 将多个图片合并成TIFF文件的两种方法
  6. 什么是串口服务器?串口服务器都用在哪些领域?
  7. python展开 c函数中的宏预处理_C 语言常用的预处理-宏函数
  8. php框架大全图解_PHP框架汇总 - 鱼煎的个人空间 - OSCHINA - 中文开源技术交流社区...
  9. Emgu-WPF学习使用-阈值化
  10. 26留数及其应用(二)
  11. 孙鑫VC学习笔记:第十六讲 (一) 利用事件对象实现线程间的同步
  12. (附源码)计算机毕业设计SSM旅游网站设计
  13. 漆包线规格型号(漆包线外径)
  14. Java集合源码剖析——基于JDK1.8中LinkedList的实现原理
  15. CentOS7安装杀毒软件ClamAV图形界面ClamTk
  16. web服务器超过访问上限显示,当Web服务器访问人数超过了设计访问人数上限,将可能出现的HTTP状态...
  17. centos7.4源码安装nginx-1.16.1 及NGINX最全配置 缓存缩略图4层转发
  18. java 返回类对象_JAVA如何实现返回不同类型的对象
  19. Linux 生产环境搭建
  20. 用bat执行ps1脚本

热门文章

  1. 32位ubuntu 使用pae
  2. ASP.NET2.0的快速入门站点
  3. [转载] Java 单例(Singleton)类
  4. Java调用Javascript、Python算法总结
  5. javascript——js string 转 int 注意的问题——parseInt(转)
  6. 如何选择使用IEnumerable, ICollection, IList
  7. nginx.conf添加lua.conf配置
  8. sdut2355Binary Search Heap Construction
  9. ffmpeg常用数据结构4
  10. android Fragments (Android官方文档中文版)