Java-使用System类的getProperties()方法获取系统属性

110 阅读3分钟

0-编辑历史

2023-11-23, 查看你的系统属性

宜朝北编程 没有我写不了的需求,只有我看不惯的产品经理

1-参考资料

docs.oracle.com/javase/8/do…

2-前置条件

有两种选择:

1、创建一个Spring Boot应用程序,添加 spring-weblombok 依赖 (我的选择)

2、一个简单的Java程序,提取核心代码,将如下代码中的 log.info 替换为 system.out.println,

3-getProperties()方法使用

编写一个控制器接口,完成的代码如下

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 今天又是吃菜的一天
 * @version 1.0
 * @since 2023/11/23
 */
@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController {

    @GetMapping("/information")
    public void getSystemInformation() throws Exception {
        // Java Runtime Environment version
        String javaVersion = System.getProperty("java.version");
        log.info("使用System.getProperty()获取到的Java版本: {} ", javaVersion);

        // Java Runtime Environment vendor
        String javaVendor = System.getProperty("java.vendor");
        log.info("使用System.getProperty()获取到的Java供应商: {} ", javaVendor);

        // Java vendor URL
        String javaVendorUrl = System.getProperty("java.vendor.url");
        log.info("使用System.getProperty()获取到的Java供应商URL: {} ", javaVendorUrl);

        // Java installation directory
        String javaHome = System.getProperty("java.home");
        log.info("使用System.getProperty()获取到的Java安装目录: {} ", javaHome);

        // Java Virtual Machine specification version
        String jvmSpecificationVersion = System.getProperty("java.vm.specification.version");
        log.info("使用System.getProperty()获取到的Java虚拟机规范版本: {} ", jvmSpecificationVersion);

        // Java Runtime Environment specification maintenance version
        String jreSpecificationMaintenanceVersion = System.getProperty("java.specification.maintenance.version");
        log.info("使用System.getProperty()获取到的Java运行时环境规范维护版本: {} ", jreSpecificationMaintenanceVersion);

        // Java Virtual Machine specification vendor
        String jvmSpecificationVendor = System.getProperty("java.vm.specification.vendor");
        log.info("使用System.getProperty()获取到的Java虚拟机规范供应商: {} ", jvmSpecificationVendor);

        // Java Virtual Machine specification name
        String jvmSpecificationName = System.getProperty("java.vm.specification.name");
        log.info("使用System.getProperty()获取到的Java虚拟机规范名称: {} ", jvmSpecificationName);

        // Java Virtual Machine implementation version
        String jvmVersion = System.getProperty("java.vm.version");
        log.info("使用System.getProperty()获取到的Java虚拟机实现版本: {} ", jvmVersion);

        // Java Virtual Machine implementation vendor
        String jvmVendor = System.getProperty("java.vm.vendor");
        log.info("使用System.getProperty()获取到的Java虚拟机实现供应商: {} ", jvmVendor);

        // Java Virtual Machine implementation name
        String jvmName = System.getProperty("java.vm.name");
        log.info("使用System.getProperty()获取到的Java虚拟机实现名称: {} ", jvmName);

        // Java Runtime Environment specification version
        String jreSpecificationVersion = System.getProperty("java.specification.version");
        log.info("使用System.getProperty()获取到的Java运行时环境规范版本: {} ", jreSpecificationVersion);

        // Java Runtime Environment specification vendor
        String jreSpecificationVendor = System.getProperty("java.specification.vendor");
        log.info("使用System.getProperty()获取到的Java运行时环境规范供应商: {} ", jreSpecificationVendor);

        // Java Runtime Environment specification name
        String jreSpecificationName = System.getProperty("java.specification.name");
        log.info("使用System.getProperty()获取到的Java运行时环境规范名称: {} ", jreSpecificationName);

        // Java class format version number
        String classVersion = System.getProperty("java.class.version");
        log.info("使用System.getProperty()获取到的Java类格式版本号: {} ", classVersion);

        // Java class path
        String classPath = System.getProperty("java.class.path");
        log.info("使用System.getProperty()获取到的Java类路径: {} ", classPath);

        // List of paths to search when loading libraries
        String libraryPath = System.getProperty("java.library.path");
        log.info("使用System.getProperty()获取到的库路径列表: {} ", libraryPath);

        // Default temp file path
        String tempDir = System.getProperty("java.io.tmpdir");
        log.info("使用System.getProperty()获取到的默认临时文件路径: {} ", tempDir);

        // Name of JIT compiler to use
        String jitCompiler = System.getProperty("java.compiler");
        log.info("使用System.getProperty()获取到的JIT编译器名称: {} ", jitCompiler);

        // Operating system name
        String osName = System.getProperty("os.name");
        log.info("使用System.getProperty()获取到的操作系统名称: {} ", osName);

        // Operating system architecture
        String osArch = System.getProperty("os.arch");
        log.info("使用System.getProperty()获取到的操作系统架构: {} ", osArch);

        // Operating system version
        String osVersion = System.getProperty("os.version");
        log.info("使用System.getProperty()获取到的操作系统版本: {} ", osVersion);

        // File separator
        String fileSeparator = System.getProperty("file.separator");
        log.info("使用System.getProperty()获取到的文件分隔符: {} ", fileSeparator);

        // Path separator
        String pathSeparator = System.getProperty("path.separator");
        log.info("使用System.getProperty()获取到的路径分隔符: {} ", pathSeparator);

        // Line separator
        String lineSeparator = System.getProperty("line.separator");
        log.info("使用System.getProperty()获取到的行分隔符: {} ", lineSeparator);

        // User's account name
        String userAccountName = System.getProperty("user.name");
        log.info("使用System.getProperty()获取到的用户帐户名: {} ", userAccountName);

        // User's home directory
        String userHomeDirectory = System.getProperty("user.home");
        log.info("使用System.getProperty()获取到的用户主目录: {} ", userHomeDirectory);

        // User's current working directory
        String currentWorkingDirectory = System.getProperty("user.dir");
        log.info("使用System.getProperty()获取到的用户当前工作目录: {} ", currentWorkingDirectory);
    }
}

4-看一看你的输出

这就是全部了,这是我在学习 System 类时整的小活,继续探索中……,have fun