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!");
}
The primary concept in retrieving the Serial Number of the Motherboard is to run commands in the terminal using the Java code and storing the retrieved Serial Number as a String which is then printed on the screen. First, we store the command that should have been run on terminal in a variable called command.
Step 1: Press Windows plus R key to get the Run window. Then type msinfo32 and press OK button to get the Windows information page. Step 2: Check the information in the System Summary page. Windows PowerShell is also an available solution to find out motherboard model and serial number.
1. Add namespace for ManagementObject 2. Add this founction which return the serial number of the machine on which the exe is executed. The content must be between 30 and 50000 characters.
CPU Serial Number (or Processor Serial Number) is a software-readable unique serial number that Intel has stamped into its Pentium 3 microprocessor. Intel offers this as a feature that can be optionally used to provide certain network management and e-commerce benefits. Basically, it lets a program identify individual PCs. Attention reader!
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);
}
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With