Javaでハードウェア情報を取得する方法

一、OS情報の取得

public String getOperatingSystemName() {
    return System.getProperty("os.name").toLowerCase();
}

二、Windows環境でのハードウェア情報取得

1. コマンド実行用のメソッド

public List<String> executeWindowsCommand(String command) {
    List<String> outputLines = new ArrayList<>();
    try {
        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", command);
        Process process = pb.start();
        
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                outputLines.add(line);
            }
        }
        
        process.waitFor();
    } catch (Exception e) {
        System.err.println("コマンド実行エラー: " + e.getMessage());
    }
    return outputLines;
}

2. ネットワークカードMACアドレスの取得

public String getNetworkCardMAC(String adapterName) {
    List<String> systemInfo = executeWindowsCommand("getmac /v");
    
    for (String info : systemInfo) {
        if (info.contains(adapterName)) {
            String[] parts = info.split("\\s+");
            if (parts.length > 2) {
                return parts[2];
            }
        }
    }
    return "不明";
}

3. CPU識別子の取得

public String getProcessorIdentifier() {
    List<String> cpuInfo = executeWindowsCommand("wmic cpu get ProcessorId");
    
    for (String info : cpuInfo) {
        if (!info.isEmpty() && !info.equals("ProcessorId")) {
            return info.trim();
        }
    }
    return "不明";
}

4. システムUUIDの取得

public String getSystemUUID() {
    List<String> uuidInfo = executeWindowsCommand("wmic csproduct get uuid");
    
    for (String info : uuidInfo) {
        if (!info.isEmpty() && !info.equals("UUID")) {
            return info.trim();
        }
    }
    return "不明";
}

三、Linux環境でのハードウェア情報取得

1. コマンド実行用のメソッド

public List<String> executeLinuxCommand(String command) {
    List<String> outputLines = new ArrayList<>();
    try {
        ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", command);
        Process process = pb.start();
        
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                outputLines.add(line);
            }
        }
        
        process.waitFor();
    } catch (Exception e) {
        System.err.println("コマンド実行エラー: " + e.getMessage());
    }
    return outputLines;
}

2. ネットワークカードMACアドレスの取得

public String getNetworkInterfaceMAC(String interfaceName) {
    List<String> interfaceInfo = executeLinuxCommand(
        "ip link show " + interfaceName + " | grep link/ether | awk '{print $2}'");
    
    if (!interfaceInfo.isEmpty()) {
        return interfaceInfo.get(0);
    }
    return "不明";
}

3. CPU識別子の取得

public String getCpuIdentifier() {
    List<String> cpuInfo = executeLinuxCommand(
        "cat /proc/cpuinfo | grep 'serial' | awk '{print $3}' | head -n 1");
    
    if (!cpuInfo.isEmpty()) {
        return cpuInfo.get(0);
    }
    return "不明";
}

4. システムUUIDの取得

public String getMachineUUID() {
    List<String> uuidInfo = executeLinuxCommand("cat /sys/class/dmi/id/product_uuid");
    
    if (!uuidInfo.isEmpty()) {
        return uuidInfo.get(0);
    }
    return "不明";
}

タグ: Java ハードウェア情報 Windows linux システム情報取得

6月1日 02:42 投稿