我想在文本字段(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!");
}
答案 0 :(得分:2)
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);
}
答案 1 :(得分: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提供的commandlline工具
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
答案 2 :(得分:0)
您应该使用其他形式的Runtime.exec:
String[] command = {"wmic", "baseboard", "get", "serial number"};
Process p = Runtime.getRuntime().exec(command);
答案 3 :(得分:0)
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);
}