how to get motherboard serial number on GUI (in java)
Asked Answered
G

5

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!");
    }
Gowon answered 29/2, 2016 at 11:24 Comment(4)
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.Fouquiertinville
Possible duplicate of get OS-level system informationIngratiating
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 jTextfieldGowon
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.Coercion
L
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);
    }
Lashawnda answered 29/2, 2016 at 16:37 Comment(0)
C
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("-", "");
    }
Cattegat answered 16/12, 2021 at 21:1 Comment(1)
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.Wormseed
Y
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
Yet answered 29/2, 2016 at 12:44 Comment(0)
H
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);
}
Honour answered 11/10, 2016 at 11:38 Comment(3)
For the motherboard number, use 'baseboard'.Honour
Could you please add an explanation as to why this is a solution.Norwood
@IronMonkey You could use the Scanner class as an alternative way of reading the process input stream, rather than BufferedReader. I never tried it using BufferedReader though, but Scanner got the job done for me.Honour
D
0

You should use an other form of Runtime.exec :

        String[] command = {"wmic", "baseboard", "get", "serial number"};
        Process p = Runtime.getRuntime().exec(command);
Dammar answered 29/2, 2016 at 11:46 Comment(1)
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.Dammar

© 2022 - 2024 — McMap. All rights reserved.