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

2023年 8月 29日 67.7k 0

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

JShell is a REPL (Read-Evaluate-Print-Loop) tool used to execute simple statements, evaluates it, and displays the result without a main() method. We can start it by simply type "jshell" in command-line prompt.

We need to get the system properties by using System.getProperty() and System.getProperties() methods.

In the below code snippet, we can able to display the system properties in the JShell tool by using static method property() of System class.

Snippet-1

jshell> System.getProperty("java.class.path")
$1 ==> "C:Program FilesJavajdk-9.0.4lib;C:json-jarsjson.jar;.;C:json-jarsjson-simple.jar;.;C:json-jarsgson.jar;.;C:json-jarscommons-io.jar;.;C:json-jarsjackson-core.jar;.;C:json-jarsjackson-databind.jar;.;C:json-jarsjackson-annotations.jar;.;C:json jarsflexjson.jar;.;C:json-jarsjackson-dataformat-xml.jar;.;C:json-jarsstax2-api.jar;.;C:json-jarsjackson-dataformat-csv.jar;.;C:json-jarsjavax.json.jar;.;C:json jarsjavax.json-api.jar;.;C:json-jarsjackson-module-jsonSchema.jar;.;C:json-jarsjson-lib.jar;.;C:json-jarscommons-lang.jar;.;C:json-jarscommons-logging.jar;.;"

登录后复制

在下面的代码片段中,我们必须使用扩展了Hashtable的“properties”对象。因此,可以通过在JShell工具中使用“System.getProperties().forEach((k, v)”来列出所有属性作为键/值对.

Snippet-2

jshell> System.getProperties().forEach((k, v) -> { System.out.printf("%s: %sn", k, v); })
sun.desktop: windows
awt.toolkit: sun.awt.windows.WToolkit
java.specification.version: 9
file.encoding.pkg: sun.io
sun.cpu.isalist: amd64
sun.jnu.encoding: Cp1252
java.class.path: C:Program FilesJavajdk-9.0.4lib;C:json jarsjson.jar;.;C:json jarsjson-simple.jar;.;C:json jarsgson.jar;.;C:json jarscommons-io.jar;.;C:json jarsjackson-core.jar;.;C:json jarsjackson-databind.jar;.;C:json jarsjackson-annotations.jar;.;C:json jarsflexjson.jar;.;C:json jarsjackson-dataformat-xml.jar;.;C:json jarsstax2-api.jar;.;C:json jarsjackson-dataformat-csv.jar;.;C:json jarsjavax.json.jar;.;C:json jarsjavax.json-api.jar;.;C:json jarsjackson-module-jsonSchema.jar;.;C:json jarsjson-lib.jar;.;C:json jarscommons-lang.jar;.;C:json jarscommons-logging.jar;.;
java.vm.vendor: Oracle Corporation
sun.arch.data.model: 64
user.variant:
java.vendor.url: http://java.oracle.com/
user.timezone:
os.name: Windows 8.1
java.vm.specification.version: 9
sun.java.launcher: SUN_STANDARD
user.country: US
sun.boot.library.path: C:Program FilesJavajdk-9.0.4bin
sun.java.command: jdk.jshell.execution.RemoteExecutionControl 54984
jdk.debug: release
sun.cpu.endian: little
user.home: C:UsersUser
user.language: en
java.specification.vendor: Oracle Corporation
java.home: C:Program FilesJavajdk-9.0.4
file.separator:
java.vm.compressedOopsMode: 32-bit
line.separator:
java.vm.specification.vendor: Oracle Corporation
java.specification.name: Java Platform API Specification
java.awt.graphicsenv: sun.awt.Win32GraphicsEnvironment
user.script:
sun.management.compiler: HotSpot 64-Bit Tiered Compilers
java.runtime.version: 9.0.4+11
user.name: User
path.separator: ;
os.version: 6.3
java.runtime.name: Java(TM) SE Runtime Environment
file.encoding: Cp1252
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug: http://bugreport.java.com/bugreport/
java.io.tmpdir: C:UsersUserAppDataLocalTemp
java.version: 9.0.4
user.dir: C:UsersUserDesktopJava 9 QNA
os.arch: amd64
java.vm.specification.name: Java Virtual Machine Specification
java.awt.printerjob: sun.awt.windows.WPrinterJob
sun.os.patch.level:
java.library.path: C:Program FilesJavajdk-9.0.4bin;C:WindowsSunJavabin;C
:Windowssystem32;C:Windows;C:Program FilesJavajdk-9.0.4bin;.;;.
java.vm.info: mixed mode
java.vendor: Oracle Corporation
java.vm.version: 9.0.4+11
sun.io.unicode.encoding: UnicodeLittle
java.class.version: 53.0

登录后复制

以上就是如何在Java 9的JShell中获取系统属性?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论