java获取服务器信息
引入依赖
build.gradle
compile 'net.java.dev.jna:jna:5.6.0'
compile 'net.java.dev.jna:jna-platform:5.6.0'
compile 'com.github.oshi:oshi-core:5.3.7'
compile 'cn.hutool:hutool-all:5.5.6'
mvaen
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.6</version>
</dependency>
代码
private String format(long size) {
if (size < 1024) {
return size + "B";
} else {
size = size / 1024;
}
if (size < 1024) {
return size + "KB";
} else {
size = size / 1024;
}
if (size < 1024) {
size = size * 100;
return size / 100 + "." + size % 100 + "MB";
} else {
size = size * 100 / 1024;
return size / 100 + "." + size % 100 + "GB";
}
}
@Test
public void info() {
Console.log("======= 操作系统信息 ======");
OsInfo osInfo = SystemUtil.getOsInfo();
Console.log("操作系统:{}", osInfo.getName());
Console.log("系统版本:{}", osInfo.getVersion());
Console.log("系统架构:{}", osInfo.getArch());
Console.log("JVM总内存:{}", format(SystemUtil.getTotalMemory()));
Console.log("JVM剩余内存:{}", format(SystemUtil.getFreeMemory()));
Console.log("======= java信息 ======");
JavaRuntimeInfo runtimeInfo = SystemUtil.getJavaRuntimeInfo();
Console.log("java版本:{}", runtimeInfo.getVersion());
Console.log("java架构:{}", runtimeInfo.getSunArchDataModel());
Console.log("java目录:{}", runtimeInfo.getHomeDir());
SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hardware = systemInfo.getHardware();
OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
Console.log("======= CPU信息 ======");
CentralProcessor cpu = hardware.getProcessor();
CentralProcessor.ProcessorIdentifier cpuInfo = cpu.getProcessorIdentifier();
Console.log("cpu数量:{}", cpu.getPhysicalPackageCount());
Console.log("cpu核心数:{}", cpu.getPhysicalProcessorCount());
Console.log("cpu线程数:{}", cpu.getLogicalProcessorCount());
Console.log("cpuID:{}", cpuInfo.getProcessorID());
Console.log("cpu名称:{}", cpuInfo.getName());
Console.log("cpu标识:{}", cpuInfo.getIdentifier());
Console.log("======= 主板信息 ======");
ComputerSystem computer = hardware.getComputerSystem();
Console.log("主板型号:{}", computer.getModel());
Console.log("主板序列号:{}", computer.getSerialNumber());
Console.log("======= 内存信息 ======");
GlobalMemory memory = hardware.getMemory();
Console.log("内存大小:{}", format(memory.getTotal()));
Console.log("可用内存:{}", format(memory.getAvailable()));
Console.log("==========");
List<PhysicalMemory> memoryList = memory.getPhysicalMemory();
memoryList.forEach((item) -> {
Console.log("内存型号:{}", item.getManufacturer());
Console.log("内存规格:{}", item.getMemoryType());
Console.log("内存主频:{}", format(item.getClockSpeed()));
Console.log("内存大小:{}", format(item.getCapacity()));
Console.log("==========");
});
Console.log("======= 物理磁盘信息 ======");
List<HWDiskStore> diskList = hardware.getDiskStores();
diskList.forEach((disk) -> {
Console.log("名称:{}", disk.getName());
Console.log("型号:{}", disk.getModel());
Console.log("序号:{}", disk.getSerial());
Console.log("大小:{}", format(disk.getSize()));
Console.log("==========");
});
Console.log("======= 逻辑磁盘信息 ======");
FileSystem fileSystem = operatingSystem.getFileSystem();
List<OSFileStore> fileList = fileSystem.getFileStores(true);
fileList.forEach((file) -> {
Console.log("名称:{}", file.getName());
Console.log("UUID:{}", file.getUUID());
Console.log("盘符:{}", file.getMount());
Console.log("文件类型:{}", file.getType());
Console.log("总大小:{}", format(file.getTotalSpace()));
Console.log("剩余大小:{}", format(file.getUsableSpace()));
Console.log("==========");
});
}
输出
======= 操作系统信息 ======
操作系统:Windows 10
系统版本:10.0
系统架构:amd64
JVM总内存:243.0MB
JVM剩余内存:189.0MB
======= java信息 ======
java版本:1.8.0_221-b11
java架构:64
java目录:C:\app\Java\x64\jdk1.8.0_221\jre
======= CPU信息 ======
cpu数量:1
cpu核心数:4
cpu线程数:8
cpuID:BFEBFBFF000906E9
cpu名称:Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
cpu标识:Intel64 Family 6 Model 158 Stepping 9
======= 主板信息 ======
主板型号:Inspiron 15 7000 Gaming
主板序列号:58D9Y32
======= 内存信息 ======
内存大小:15.86GB
可用内存:5.18GB
==========
内存型号:Samsung
内存规格:DDR4
内存主频:2.23GB
内存大小:8.0GB
==========
内存型号:SK Hynix
内存规格:DDR4
内存主频:2.23GB
内存大小:8.0GB
==========
======= 物理磁盘信息 ======
19:22:14.526 [main] DEBUG oshi.util.platform.windows.PerfCounterQuery - Localized PhysicalDisk to PhysicalDisk
名称:\\.\PHYSICALDRIVE0
型号:KINGSTON SUV500M8240G (标准磁盘驱动器)
序号:50026B768258ACA9
大小:223.56GB
==========
名称:\\.\PHYSICALDRIVE1
型号:TOSHIBA MQ01ABD100 (标准磁盘驱动器)
序号: 87C3S6KZS
大小:931.51GB
==========
======= 逻辑磁盘信息 ======
名称:本地固定磁盘 (C:)
UUID:e4769774-92f9-431e-ad1d-96963d9d03bc
盘符:C:\
文件类型:NTFS
总大小:221.37GB
剩余大小:88.8GB
==========
名称:本地固定磁盘 (D:)
UUID:81c881c4-76ff-48af-b0c1-2070f753310f
盘符:D:\
文件类型:NTFS
总大小:300.0GB
剩余大小:218.97GB
==========
名称:本地固定磁盘 (E:)
UUID:a689f086-7265-4c3e-b622-8a641787dd5e
盘符:E:\
文件类型:NTFS
总大小:300.72GB
剩余大小:136.71GB
==========
名称:本地固定磁盘 (F:)
UUID:8c126ad0-bf59-457d-8c2f-e3248e03e9f7
盘符:F:\
文件类型:NTFS
总大小:330.65GB
剩余大小:213.28GB
==========
原创内容,如果你觉得文章还可以的话,不妨点个赞支持一下!转载请注明出处。