java 中获取系统信息(cpu、内存、硬盘、操作系统)主要有以下几种方式:
- 使用 java 原生 api(
java.lang.management包) - 使用第三方库(如 oshi)
- 使用 runtime 执行系统命令
下面详细介绍每种方法。
一、使用 java 原生 api
java 自带的 managementfactory 可以获取部分系统信息,但功能有限。
1. 获取操作系统信息
import java.lang.management.managementfactory;
import java.lang.management.operatingsystemmxbean;
public class systeminfo {
public static void main(string[] args) {
operatingsystemmxbean osbean = managementfactory.getoperatingsystemmxbean();
system.out.println("操作系统名称: " + osbean.getname());
system.out.println("操作系统版本: " + osbean.getversion());
system.out.println("操作系统架构: " + osbean.getarch());
system.out.println("可用处理器核心数: " + osbean.getavailableprocessors());
system.out.println("系统平均负载: " + osbean.getsystemloadaverage());
}
}2. 获取内存信息(有限)
import com.sun.management.operatingsystemmxbean;
import java.lang.management.managementfactory;
public class memoryinfo {
public static void main(string[] args) {
operatingsystemmxbean osbean = (operatingsystemmxbean) managementfactory.getoperatingsystemmxbean();
// 获取总物理内存(字节)
long totalmemory = osbean.gettotalphysicalmemorysize();
// 获取可用物理内存(字节)
long freememory = osbean.getfreephysicalmemorysize();
system.out.println("总物理内存: " + formatbytes(totalmemory));
system.out.println("可用物理内存: " + formatbytes(freememory));
system.out.println("已用物理内存: " + formatbytes(totalmemory - freememory));
}
private static string formatbytes(long bytes) {
return string.format("%.2f gb", bytes / (1024.0 * 1024.0 * 1024.0));
}
}注意:com.sun.management.operatingsystemmxbean 不是标准 api,在不同 jdk 实现中可能不可用。
二、使用 oshi 库(推荐)
oshi(operating system and hardware information)是一个跨平台的 java 库,功能最全面。
1. 添加 maven 依赖
<dependency>
<groupid>com.github.oshi</groupid>
<artifactid>oshi-core</artifactid>
<version>6.4.0</version>
</dependency>2. 获取 cpu 信息
import oshi.systeminfo;
import oshi.hardware.centralprocessor;
import oshi.hardware.hardwareabstractionlayer;
public class cpuinfo {
public static void main(string[] args) {
systeminfo si = new systeminfo();
hardwareabstractionlayer hal = si.gethardware();
centralprocessor cpu = hal.getprocessor();
system.out.println("cpu 名称: " + cpu.getprocessoridentifier().getname());
system.out.println("cpu 物理核心数: " + cpu.getphysicalprocessorcount());
system.out.println("cpu 逻辑核心数: " + cpu.getlogicalprocessorcount());
system.out.println("cpu 最大频率: " + cpu.getmaxfreq() / 1_000_000 + " mhz");
// 获取 cpu 使用率
long[] prevticks = cpu.getsystemcpuloadticks();
try {
thread.sleep(1000); // 等待1秒计算使用率
} catch (interruptedexception e) {
e.printstacktrace();
}
double cpuload = cpu.getsystemcpuloadbetweenticks(prevticks) * 100;
system.out.printf("cpu 使用率: %.2f%%\n", cpuload);
}
}3. 获取内存信息
import oshi.systeminfo;
import oshi.hardware.globalmemory;
import oshi.hardware.hardwareabstractionlayer;
public class memoryinfo {
public static void main(string[] args) {
systeminfo si = new systeminfo();
hardwareabstractionlayer hal = si.gethardware();
globalmemory memory = hal.getmemory();
long total = memory.gettotal();
long available = memory.getavailable();
system.out.println("总内存: " + formatbytes(total));
system.out.println("可用内存: " + formatbytes(available));
system.out.println("已用内存: " + formatbytes(total - available));
system.out.println("内存使用率: " +
string.format("%.2f%%", (total - available) * 100.0 / total));
// 获取交换空间(虚拟内存)
system.out.println("总交换空间: " + formatbytes(memory.getvirtualmemory().getswaptotal()));
system.out.println("可用交换空间: " + formatbytes(memory.getvirtualmemory().getswapfree()));
}
private static string formatbytes(long bytes) {
return string.format("%.2f gb", bytes / (1024.0 * 1024.0 * 1024.0));
}
}4. 获取硬盘信息
import oshi.systeminfo;
import oshi.hardware.hwdiskstore;
import oshi.hardware.hardwareabstractionlayer;
import java.util.list;
public class diskinfo {
public static void main(string[] args) {
systeminfo si = new systeminfo();
hardwareabstractionlayer hal = si.gethardware();
list<hwdiskstore> diskstores = hal.getdiskstores();
for (hwdiskstore disk : diskstores) {
system.out.println("硬盘名称: " + disk.getname());
system.out.println("硬盘型号: " + disk.getmodel());
system.out.println("硬盘序列号: " + disk.getserial());
system.out.println("硬盘大小: " + formatbytes(disk.getsize()));
system.out.println("读取次数: " + disk.getreads());
system.out.println("写入次数: " + disk.getwrites());
system.out.println("读取字节数: " + formatbytes(disk.getreadbytes()));
system.out.println("写入字节数: " + formatbytes(disk.getwritebytes()));
system.out.println("----------------------------");
}
// 获取文件系统信息(分区)
hal.getfilesystem().getfilestores().foreach(filestore -> {
system.out.println("分区: " + filestore.getvolume());
system.out.println("挂载点: " + filestore.getmount());
system.out.println("总空间: " + formatbytes(filestore.gettotalspace()));
system.out.println("可用空间: " + formatbytes(filestore.getusablespace()));
system.out.println("已用空间: " + formatbytes(filestore.gettotalspace() - filestore.getusablespace()));
system.out.println("----------------------------");
});
}
private static string formatbytes(long bytes) {
return string.format("%.2f gb", bytes / (1024.0 * 1024.0 * 1024.0));
}
}5. 获取操作系统信息
import oshi.systeminfo;
import oshi.software.os.operatingsystem;
public class osinfo {
public static void main(string[] args) {
systeminfo si = new systeminfo();
operatingsystem os = si.getoperatingsystem();
system.out.println("操作系统: " + os.tostring());
system.out.println("操作系统家族: " + os.getfamily());
system.out.println("操作系统制造商: " + os.getmanufacturer());
system.out.println("操作系统版本: " + os.getversioninfo());
system.out.println("进程数: " + os.getprocesscount());
system.out.println("线程数: " + os.getthreadcount());
system.out.println("系统启动时间: " + new java.util.date(os.getsystemboottime() * 1000));
}
}6. 完整示例:获取所有信息
import oshi.systeminfo;
import oshi.hardware.*;
import oshi.software.os.operatingsystem;
public class allsysteminfo {
public static void main(string[] args) {
systeminfo si = new systeminfo();
hardwareabstractionlayer hal = si.gethardware();
operatingsystem os = si.getoperatingsystem();
system.out.println("========== 操作系统信息 ==========");
system.out.println("操作系统: " + os.tostring());
system.out.println("系统启动时间: " + new java.util.date(os.getsystemboottime() * 1000));
system.out.println("\n========== cpu信息 ==========");
centralprocessor cpu = hal.getprocessor();
system.out.println("cpu: " + cpu.getprocessoridentifier().getname());
system.out.println("物理核心: " + cpu.getphysicalprocessorcount());
system.out.println("逻辑核心: " + cpu.getlogicalprocessorcount());
// cpu 使用率
long[] prevticks = cpu.getsystemcpuloadticks();
try { thread.sleep(1000); } catch (interruptedexception e) { }
system.out.printf("cpu使用率: %.2f%%\n", cpu.getsystemcpuloadbetweenticks(prevticks) * 100);
system.out.println("\n========== 内存信息 ==========");
globalmemory memory = hal.getmemory();
long totalmem = memory.gettotal();
long availmem = memory.getavailable();
system.out.println("总内存: " + formatbytes(totalmem));
system.out.println("可用内存: " + formatbytes(availmem));
system.out.printf("内存使用率: %.2f%%\n", (totalmem - availmem) * 100.0 / totalmem);
system.out.println("\n========== 硬盘信息 ==========");
hal.getfilesystem().getfilestores().foreach(fs -> {
system.out.println("分区: " + fs.getmount());
system.out.println("总空间: " + formatbytes(fs.gettotalspace()));
system.out.println("可用空间: " + formatbytes(fs.getusablespace()));
system.out.println("-------------------");
});
}
private static string formatbytes(long bytes) {
string[] units = {"b", "kb", "mb", "gb", "tb"};
int unitindex = 0;
double size = bytes;
while (size >= 1024 && unitindex < units.length - 1) {
size /= 1024;
unitindex++;
}
return string.format("%.2f %s", size, units[unitindex]);
}
}三、使用 runtime 执行系统命令
如果不想引入第三方库,可以通过执行系统命令来获取信息。
windows 示例
import java.io.bufferedreader;
import java.io.inputstreamreader;
public class commandinfo {
public static void main(string[] args) {
// 获取 cpu 信息
executecommand("wmic cpu get name");
// 获取内存信息
executecommand("wmic memorychip get capacity");
// 获取硬盘信息
executecommand("wmic logicaldisk get size,freespace,caption");
}
private static void executecommand(string command) {
try {
process process = runtime.getruntime().exec(command);
bufferedreader reader = new bufferedreader(
new inputstreamreader(process.getinputstream(), "gbk"));
string line;
while ((line = reader.readline()) != null) {
system.out.println(line);
}
process.waitfor();
} catch (exception e) {
e.printstacktrace();
}
}
}linux 示例
// cpu 信息
executecommand("cat /proc/cpuinfo | grep 'model name' | head -1");
// 内存信息
executecommand("cat /proc/meminfo | grep memtotal");
// 硬盘信息
executecommand("df -h");总结
| 方法 | 优点 | 缺点 |
|---|---|---|
| java 原生 api | 无需额外依赖 | 信息有限,部分 api 非标准 |
| oshi 库 | 跨平台,信息全面,易于使用 | 需要引入第三方依赖 |
| 执行系统命令 | 能获取任意信息 | 平台相关,解析复杂,性能较差 |
推荐使用 oshi 库,它封装了底层细节,提供了统一的跨平台 api,能获取最全面的系统信息。
到此这篇关于java一键获取(cpu、内存、硬盘、操作系统)系统信息的文章就介绍到这了,更多相关java获取系统信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论