背景

今日搬砖?时需要获取系统运行时间、版本号等相关信息,使用Java自带的类进行获取系统运行的相关信息,在这整理记录分享一下,感兴趣的小伙伴可以自己尝试尝试。

Jvm

首先获取jvm相关信息,包含jvm的名称、版本号、启动时间、运行时间、环境变量、进程id等等

public class Test {
    public static void main(String[] args) {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        // jvmName
        System.out.printf("jvmName: %s %n", runtimeMXBean.getVmName());
        // jvmVersion
        System.out.printf("jvmVersion: %s %n", runtimeMXBean.getVmVersion());
        // jvmVendor
        System.out.printf("jvmVendor: %s %n", runtimeMXBean.getVmVendor());
        // startTime 使用hutool中DateUtil进行转换
        long startTime = runtimeMXBean.getStartTime();
        System.out.printf("startTime: %s %n", DateUtil.date(startTime).toString());
        // updateTime
        long uptime = runtimeMXBean.getUptime();
        System.out.printf("updateTime: %s %n", DateUtil.formatBetween(uptime, BetweenFormater.Level.SECOND));
        // classPath
        System.out.printf("classPath: %s %n", runtimeMXBean.getClassPath());
        // systemProperties
        System.out.printf("jvmName: %s %n", runtimeMXBean.getSystemProperties());
        // bootClassPath
        System.out.printf("bootClassPath: %s %n", runtimeMXBean.getBootClassPath());
        // processId
        System.out.printf("processId: %s %n", runtimeMXBean.getName().split("@")[0]);
    }
}

还可以获取JVM内存相关信息,例如堆内存。

public class Test {
    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        // heapMemoryUsed
        System.out.printf("heapMemoryUsed: %d MB %n", heapMemoryUsage.getUsed()/1024/1024);
        // heapMemoryMax
        System.out.printf("heapMemoryMax: %d MB %n", heapMemoryUsage.getMax()/1024/1024);
        // heapMemoryCommitted
        System.out.printf("heapMemoryCommitted: %d MB %n", heapMemoryUsage.getCommitted()/1024/1024);
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        // nonHeapMemoryUsed
        System.out.printf("nonHeapMemoryUsed: %d MB %n", nonHeapMemoryUsage.getUsed()/1024/1024);
        // nonHeapMemoryMax
        System.out.printf("nonHeapMemoryMax: %d MB %n", nonHeapMemoryUsage.getMax()/1024/1024);
        // nonHeapMemoryCommitted
        System.out.printf("nonHeapMemoryCommitted: %d MB %n", nonHeapMemoryUsage.getCommitted()/1024/1024);
    }
}

获取JDK相关信息。包含jdk的版本、安装路径、当前运行jar包路径、运行jar文件名等。

public class Test {
    public static void main(String[] args) {
        // jdkVersion
        System.out.printf("jdkVersion: %s %n", System.getProperty("java.version"));
        // java_home
        System.out.printf("java_home: %s %n", System.getProperty("java.home"));
        // jar包路径
        String path = System.getProperty("java.class.path");
        int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
        int lastIndex = path.lastIndexOf(File.separator) + 1;
        String jarPath = path.substring(firstIndex, lastIndex);
        System.out.printf("jarPath: %s %n", jarPath);
        // 当前运行jar文件名
        String jarName = path.substring(lastIndex);
        System.out.printf("jarName: %s %n", jarName);
    }
}

获取java虚拟机线程信息,包含线程的阻塞时间、次数、线程的堆栈信息等等。

public class Test {
    public static void main(String[] args) {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        // ThreadCount
        System.out.printf("ThreadCount: %d %n", threadMXBean.getThreadCount());
        // AllThreadIds
        System.out.printf("AllThreadIds: %s %n", threadMXBean.getAllThreadIds());
        // TotalStartedThread
        System.out.printf("TotalStartedThread: %d %n", threadMXBean.getTotalStartedThreadCount());
        // DaemonThread
        System.out.printf("DaemonThread: %d %n", threadMXBean.getDaemonThreadCount());
        // PeakThread
        System.out.printf("PeakThread: %d %n", threadMXBean.getPeakThreadCount());
        // ThreadInfo
        System.out.printf("ThreadInfo: %s %n", threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds()));
    }
}

获取java虚拟机类加载相关信息。

public class Test {
    public static void main(String[] args) {
        ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
        // LoadedClassCount
        System.out.printf("LoadedClassCount: %d %n", classLoadingMXBean.getLoadedClassCount());
        // TotalLoadedClassCount
        System.out.printf("TotalLoadedClassCount: %d %n", classLoadingMXBean.getTotalLoadedClassCount());
        // UnloadedClassCount
        System.out.printf("UnloadedClassCount: %d %n", classLoadingMXBean.getUnloadedClassCount());
    }
}

操作系统

获取操作系统以及主机硬件信息,包含系统名称、版本、物理内存、可用内存等等。

public class Test {
    public static void main(String[] args) {
        // 系统版本
        OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactoryHelper.getOperatingSystemMXBean();
        // osName
        System.out.printf("osName: %s %n", operatingSystemMXBean.getName());
        // Arch
        System.out.printf("Arch: %s %n", operatingSystemMXBean.getArch());
        // Version
        System.out.printf("Version: %s %n", operatingSystemMXBean.getVersion());
        // 物理内存
        long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize()/1024/1024/1024;
        // totalPhysicalMemorySize
        System.out.printf("totalPhysicalMemorySize: %d GB %n", totalPhysicalMemorySize);
    }
}
原文地址:https://www.cnblogs.com/aibianchengya/archive/2022/10/18/16802821.html

相关文章: