6

I did like to show a motherboard serial number in text field (GUI panel). I created a text field and action button. I wrote this code in action button. What mistake did i make in this code?

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!");
    }
Alamin Dawan
  • 119
  • 1
  • 2
  • 12
  • You should not ignore the exception. At least write it to a log. In this case it would tell you the reason for the error. – blafasel Feb 29 '16 at 11:54
  • Possible duplicate of [get OS-level system information](http://stackoverflow.com/questions/25552/get-os-level-system-information) – vzamanillo Feb 29 '16 at 12:20
  • it could not tell me what is exception. when i run it in console then it work well but when ever i am try it gui then not working and don't display in jTextfield – Alamin Dawan Feb 29 '16 at 12:25
  • I think getting output from command line tools and parsing their info is rubbish. Get a library which is able to query WMI from Java and use that one. – Marged Mar 01 '16 at 08:38

5 Answers5

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);
    }
Aqeel Haider
  • 603
  • 7
  • 24
2
    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("-", "");
    }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 08:06
1

The problem is that you read a multi-line output

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

but you store always only the line which was current read in the textfield. Means previous output is overwritten.

...
        motherboard.setText(line);
}

As the last line of the output is an empty line your text field shows this empty line (means you don't see any output).

edit Below is added only for completeness.

A small method which could be used as String serialNumber = getSerialNumber(). It filter out the header line and the empty lines.

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

Another way could be to do the filtering already on the wmic command and read only the first line from the output.

Either with commandlline tools provided by Windows

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

or using a custom XSL to control the output of wmic.

Save it as 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>

and run the command as

wmic baseboard get serialnumber /Format:.\simple
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
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);
}
0

You should use an other form of Runtime.exec :

        String[] command = {"wmic", "baseboard", "get", "serial number"};
        Process p = Runtime.getRuntime().exec(command);
Xvolks
  • 2,065
  • 1
  • 21
  • 32
  • Often when you invoke an external command with parameters, the `exec(String)` method does not work as expected. The `exec(String[])` version is much more reliable in those cases. – Xvolks Feb 29 '16 at 15:09