6

我确实想在文本字段(GUI 面板)中显示主板序列号。我创建了一个文本字段和操作按钮。我在操作按钮中编写了此代码。我在这段代码中犯了什么错误?

try {
        Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
        BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));

        while (true) {

            String line = inn.readLine();
            if (line == null) {
                break;
            }
            motherboard.setText(line);
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Sorry could not found motherboard serial!");
    }
4

5 回答 5

3
 try
    {
        String result = null;
        Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
        BufferedReader input
                = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null)
        {
            result += line;
        }
        if (result.equalsIgnoreCase(" ")) {
            System.out.println("Result is empty");
        } else
        {
            motherboard.setText(result);
        }
        input.close();
    } catch (IOException ex)
    {
        Exceptions.printStackTrace(ex);
    }
于 2016-02-29T16:37:06.000 回答
1

问题是您读取了multi-line输出

while (true) {
    String line = inn.readLine();
    if (line == null) {
            break;
        }
...

但是您始终只存储当前在文本字段中读取的行。表示先前的输出被覆盖。

...
        motherboard.setText(line);
}

由于输出的最后一行是empty line您的文本字段显示此空行(意味着您看不到任何输出)。

编辑下面添加仅是为了完整性。

一个小方法,可以作为String serialNumber = getSerialNumber(). 它过滤掉标题行和空行。

static String getSerialNumber() throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder("wmic", "baseboard", 
            "get", "serialnumber");
    Process process = pb.start();
    process.waitFor();
    String serialNumber = "";
    try (BufferedReader br = new BufferedReader(new InputStreamReader(
            process.getInputStream()))) {
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            if (line.length() < 1 || line.startsWith("SerialNumber")) {
                continue;
            }
            serialNumber = line;
            break;
        }
    }
    return serialNumber;
}

另一种方法可能是已经在wmic命令上进行过滤,并且只读取输出的第一行。

使用 Windows 提供的命令行工具

wmic baseboard get serialnumber | findstr /r /v "^$" | findstr /v "SerialNumber"

或使用自定义XSL来控制wmic.

另存为simple.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/"><xsl:apply-templates select="COMMAND/RESULTS"/>
</xsl:template>
</xsl:stylesheet>

并将命令运行为

wmic baseboard get serialnumber /Format:.\simple
于 2016-02-29T12:44:45.763 回答
1
private void serial(){
    // wmic command for diskdrive id: wmic DISKDRIVE GET SerialNumber
    // wmic command for cpu id : wmic cpu get ProcessorId
    Process process = null;
    try {
        process = Runtime.getRuntime().exec(new String[] { "wmic", "bios", "get", "SerialNumber" });
        //process = Runtime.getRuntime().exec(new String[] { "wmic", "DISKDRIVE", "get", "SerialNumber" });
       // process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" });
       //process = Runtime.getRuntime().exec(new String[] { "wmic", "baseboard", "get", "SerialNumber" });
        process.getOutputStream().close();
    } catch (IOException ex) {
        Logger.getLogger(Activate.class.getName()).log(Level.SEVERE, null, ex);
    }
    Scanner sc = new Scanner(process.getInputStream());
    String property = sc.next();
    String serial = sc.next();
    System.out.println(property + ": " + serial);
    this.serial.setText(property + ": " + serial);
}
于 2016-10-11T11:38:19.410 回答
0
    getBufferReader("wmic logicaldisk get volumeserialnumber");
    getBufferReader("wmic csproduct get UUID");
    getBufferReader("wmic csproduct get name");
    getBufferReader("wmic CPU get ProcessorId");
    getBufferReader("wmic CPU get Architecture");
    getBufferReader("wmic CPU get NumberOfCores");
    getBufferReader("wmic CPU get ProcessorType");
    getBufferReader("wmic CPU get Revision");
    getBufferReader("wmic CPU get Family");
    getBufferReader("wmic CPU get Level");
    getBufferReader("wmic CPU get Name");
    getBufferReader("wmic CPU get SerialNumber");
    getBufferReader("wmic bios get Manufacturer");
    getBufferReader("wmic bios get Version");
    getBufferReader("wmic bios get SMBIOSBIOSVersion");
    getBufferReader("wmic bios get SMBIOSMajorVersion");
    getBufferReader("wmic bios get SMBIOSMinorVersion");
    getBufferReader("wmic bios get ReleaseDate");
    getBufferReader("wmic bios get serialnumber");
    getBufferReader("wmic baseboard get serialnumber");
    getBufferReader("wmic DISKDRIVE get SerialNumber");
    getBufferReader("wmic useraccount get name,sid");


    public static String getBufferReader(String getCode) throws IOException, Exception {
        StringBuilder output = new StringBuilder();
        Process processRun = Runtime.getRuntime().exec(getCode);
        BufferedReader processBuf = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
        String line;
        while ((line = processBuf.readLine()) != null) {
            output.append(line).append("\n");
        }
        return output.toString().substring(output.indexOf("\n"), output.length()).trim().replace("-", "");
    }
于 2021-12-16T21:01:28.527 回答
0

您应该使用其他形式的 Runtime.exec :

        String[] command = {"wmic", "baseboard", "get", "serial number"};
        Process p = Runtime.getRuntime().exec(command);
于 2016-02-29T11:46:06.427 回答