how to get motherboard Serial Number? - java

For getting the motherboard serial number, I used the following code. The output is empty, however. Is there anything I can do to fix it?
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n"
+ "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
if(result.equalEgnoreCase(" ") {
System.out.println("Result is empty");
} else {
System.out.println("Result :>"+result);
}
input.close();
}

I found that the following code works.
package ui;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class MB {
public static void main(String[] args) throws IOException {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n" + " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n" + " Wscript.StdOut.Writeline objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line, result = new String();
while ((line = input.readLine()) != null) {
result += line;
}
if (result.equalsIgnoreCase(" ")) {
System.out.println("Result is empty");
} else {
System.out.println("Result :>" + result);
}
input.close();
}
}

Related

How to send a string to ffmpeg's input in Java

I am quite new to java, and try as I might, I can't find any examples to help me. I am running ffmpeg as a process and parsing the stderr to get various bits of data - all of which is working fine, but I want to send a "q\n" command to ffmpeg's input from a gui menu item to gracefully quit it whilst it is running when necessary. So all I want to do is send a string programmatically to ffmpeg, the equivalent of sending q return from terminal. Thanks in advance
edit here is the relevant (simplified) section of the code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Thread thread = new Thread() {
public void run()
BufferedReader error_reader,input_reader;
InputStreamReader error_isr,input_isr;
String ffmpeg_command = "ffmpeg " + overwrite + " -i " + "\"" + currentfilestring + "\"" + " " + stream + " " + test + " " + findsilence + " " + videocodec + " -b:v " + videoqual + " " + audiocodec + " -ac 2 -ab " + audioqual + " " + res + " " + aspectratio + " " + framerate + " " + "\"" + destdir + destfile + "\"";
System.out.println(ffmpeg_command);
try {
OutputStream stdout;
InputStream stdin;
InputStream stderr;
String errorstr,inputstr;
//Run the ffmpeg
Process ffmpeg = Runtime.getRuntime().exec(ffmpeg_command, null, new File(userDir));
//Get stdin,stderr + stdout
stdin = ffmpeg.getInputStream();
stderr = ffmpeg.getErrorStream();
stdout = ffmpeg.getOutputStream();
stdout.write("\r\n".getBytes());
stdout.flush();
stdout.close();
error_isr = new InputStreamReader(stderr);
error_reader = new BufferedReader(error_isr);
input_reader = new BufferedReader(input_isr);
while (!error_reader.ready()) {
}
while ((errorstr = error_reader.readLine()) != null) {
if(stopconv =="yes"){
//-------------------------------------------------------------------------------------
// TRYING TO INPUT "q\n" TO FFMPEG HERE
//-------------------------------------------------------------------------------------
}
error_isr.close();
error_reader.close();
stdin.close();
stderr.close();
jProgressBar1.setValue(0);
ffmpeg.destroy();
} catch (Exception e) {
e.printStackTrace();
}
};
thread.start();
}

Generate vCard from Contact Data by java

I would like to be able to generate a vCard given the data contained within person_contact.
Here is my code.But I am unable to generate the vcard properly.
private void m1() throws IOException {
Contact contact = buildContact();
File f = new File("contact.vcf");
FileOutputStream fop = new FileOutputStream(f);
String outputImageFilePath = "contact.vcf";
createDirectoryIfNotExists(outputImageFilePath);
if (f.exists()) {
String str = "BEGIN:VCARD\n" + "VERSION:4.0\n" + "N:" + contact.getFull_name() + ";;;\n"
+ "FN:" + contact.getFull_name() + "\n" + "ORG:" + contact.getOrganization_name()
+ "\n" + "TITLE:" + contact.getTitle() + "\n" + "TEL;TYPE="
+ contact.getPhone_2_type() + ";VALUE=uri:" + contact.getPhone_2() + "\n"
+ "TEL;TYPE=" + contact.getPhone_3_type() + ",voice;VALUE=uri:tel:"
+ contact.getPhone_3() + "\n" + "EMAIL:" + contact.getEmail() + "\n" + "FAX:"
+ contact.getFax() + "\n" + "STREET1:" + contact.getStreet1() + "\n" + "STREET2:"
+ contact.getStreet2() + "\n" + "CITY:" + contact.getCity() + "\n" + "STATE:"
+ contact.getState() + "\n"
+ "END:VCARD";
fop.write(str.getBytes());
// Now read the content of the vCard after writing data into it
BufferedReader br = null;
String sCurrentLine;
br = new BufferedReader(new FileReader("contact.vcf"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
System.out.println(f.getAbsolutePath());
}
// close the output stream and buffer reader
fop.flush();
fop.close();
System.out.println("The data has been written");
}
else
System.out.println("This file does not exist");
}
private static Contact buildContact() {
Contact contact = new Contact();
contact.setCity("xxxxxx");
contact.setCountry("xxxxxx");
contact.setEmail("test#xxxxxx.com");
contact.setFax("123456789");
contact.setFull_name("Dummy Name");
contact.setOrganization_name("Dummy-technologies");
contact.setPhone("123456789");
contact.setPhone_type("work");
contact.setPhone_2("987456321");
contact.setPhone_2_type("home");
contact.setPhone_3("564789451236");
contact.setPhone_3_type("work2");
contact.setPostal_code("500000");
contact.setState("state");
contact.setStreet1("street1");
contact.setStreet2("stree2");
contact.setTitle("company");
contact.setWebsite_url("www.text.com");
return contact;
}
It is not generating the vcard properly.Can any one help me plz.

Writing to .txt skipping lines and leaving white spaces java

String newPurchaseOrder = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
try {
String filename = "PurchaseOrderDataFile.txt";
FileWriter fw = new FileWriter(filename, true); //the true will append the new data
BufferedWriter bw = new BufferedWriter(fw);
FileReader fr = new FileReader("PurchaseOrderDataFile.txt");
bw.write("\n" + newPurchaseOrder);
bw.close();
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
Trying to prevent it from skipping lines when inputting to the .txt file
08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3#
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8
16/07/13#ESE-5577#ZR/7413#6
Input skips lines such as above
what do you mean "skipping lines"? bw.write("\n" + newPurchaseOrder); will first put an empty line if that is what you mean, just transfer "\n" to the end.. The following code works fine:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
class myWrite {
public static void main(String[] args) {
String dateStr = "test";
String customerID = "1";
String productCode = "100";
String qty = "1000";
String newPurchaseOrder = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
String newPurchaseOrder2 = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
try {
String filename = "PurchaseOrderDataFile.txt";
FileWriter fw = new FileWriter(filename, true); //the true will append the new data
BufferedWriter bw = new BufferedWriter(fw);
FileReader fr = new FileReader("PurchaseOrderDataFile.txt");
bw.write(newPurchaseOrder + "\n");
bw.write(newPurchaseOrder2 + "\n");
bw.close();
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
}
writes:
test#1#100#1000
test#1#100#1000
EDIT: Using the output you told me,
08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3#
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8
I then added the line you told me:
16/07/13#ESE-5577#ZR/7413#6
which produces:
08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3#
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8
16/07/13#ESE-5577#ZR/7413#6
use my code it works fine and does what you want..

How can I get my motherboard's ID, using Java, in Linux, Mac, and Solaris?

How can I get my motherboard's ID, using Java, in Linux, Mac, and Solaris? I'd prefer a cross-platform solution.
I found a way that works in Windows:
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n"
+ "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
return result.trim();
That works perfectly in Windows, but I need something that will also work in Mac, Linux and Solaris.

Find Product Id and Vendor Id of USB_FlashDrive using Java

I need help to find the product and vendor ID of USB_Flash-drive, I used the following code but it won't work,
public static String getProductId() {
// TODO Auto-generated method stub
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_USBHub WHERE\"\\\\.\\PHYSICALDRIVE0\"\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.PNPDeviceID \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}
In this above code i use this line (\"Select * from Win32_USBHub\") \n" instead of (\"Select * from Win32_USBHub WHERE\"\\\\.\\PHYSICALDRIVE0\"\") \n", which give the Product and Vendor id of my Dvd_Drive. Here what should i do for the correct result.
Try Win32_USBController
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/disk/drives/#RetrUBSHubInfo.htm

Categories