2016-02-29 22 views
3

我確實喜歡在文本字段(GUI面板)中顯示主板序列號。我創建了一個文本字段和操作按鈕。我在操作按鈕中編寫了這段代碼。我在這段代碼中犯了什麼錯誤?如何在GUI上獲取主板序列號(在java中)

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

你不應該忽略異常。至少將它寫入日誌。在這種情況下,它會告訴你錯誤的原因。 – blafasel

+0

[獲取操作系統級別的系統信息]的可能重複(http://stackoverflow.com/questions/25552/get-os-level-system-information) – vzamanillo

+0

它不能告訴我什麼是異常。當我在控制檯中運行它然後它工作正常,但是當我嘗試它gui然後不工作,並且不顯示在jTextfield –

回答

0
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); 
    } 
0

,您應該使用的Runtime.exec的其他形式:

 String[] command = {"wmic", "baseboard", "get", "serial number"}; 
     Process p = Runtime.getRuntime().exec(command); 
+0

我不明白你能清除我嗎? –

+0

通常,當您使用參數調用外部命令時,'exec(String)'方法無法按預期工作。在這種情況下,exec(String [])'版本更可靠。 – Xvolks

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命令和只讀從輸出的第一行。

要麼使用自定義XSL控制的wmic輸出由Windows

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

或提供commandlline工具。

保存爲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 
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); 
} 
+0

對於主板編號,請使用'baseboard'。 –

+0

請問您可以添加一個解釋爲什麼這是一個解決方案。 – IronMonkey

+0

@IronMonkey您可以使用Scanner類作爲讀取過程輸入流的替代方法,而不是BufferedReader。雖然我從未嘗試過使用BufferedReader,但Scanner爲我完成了這項工作。 –