Whats wrong with my code??
Hi, can any one help me whats wrong with my code? the file location is that of a usb device which has been connected to my computer system. it should in theory display the name, driver and class of the device, however i keep gettin "null" error messages. any help would be greatly appriciated.
Code :
import java.io.*;
import javax.swing.JOptionPane;
public class USBDevices
{
// Allows the registry to be viewed and opens specific value by creating strings
private static final String REGQUERY = "reg query";
private static final String REG_SZ = "REG_SZ";
// Sets the location of the folder found in the registry as a string for Device Name
private static final String FriendlyName = REGQUERY +
"\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_HTC&Prod_Android_Phone&Rev_0100\\HT091PL05241&0\" /v FriendlyName";
// Sets the location of the folder found in the registry as a string for Device Driver
private static final String DeviceDriver = REGQUERY +
"\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_HTC&Prod_Android_Phone&Rev_0100\\HT091PL05241&0\" /v Driver";
// Sets the location of the folder found in the registry as a string for Device Type
private static final String Class = REGQUERY +
"\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_HTC&Prod_Android_Phone&Rev_0100\\HT091PL05241&0\" /v Class";
// This sets a string to allow the data retrieved to have a name attatched to it
public static String getDeviceFriendlyName()
{
try
{
// This is the process what will open up the registry and retrive the data found for the device name then
// save it within the string called getDeviceFriendlyName
Process process = Runtime.getRuntime().exec(FriendlyName);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REG_SZ);
// If no data is retrived it will save the string as null
if (p == -1)
return null;
return result.substring(p + REG_SZ.length()).trim();
}
// This will allow an exception to be caught and saved as null
catch (Exception e)
{
return null;
}
}
// This sets a string to allow the data retrieved to have a name attatched to it
public static String getDeviceDriver()
{
try
{
// This is the process what will open up the registry and retrive the data found for the device name then
// save it within the string called getDeviceDriver
Process process = Runtime.getRuntime().exec(DeviceDriver);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REG_SZ);
// If no data is retrived it will save the string as null
if (p == -1)
return null;
return result.substring(p + REG_SZ.length()).trim();
}
// This will allow an exception to be caught and saved as null
catch (Exception e)
{
return null;
}
}
// This sets a string to allow the data retrieved to have a name attatched to it
public static String getDeviceClass()
{
try
{
// This is the process what will open up the registry and retrive the data found for the device name then
// save it within the string called getDeviceClass
Process process = Runtime.getRuntime().exec(Class);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REG_SZ);
// If no data is retrived it will save the string as null
if (p == -1)
return null;
return result.substring(p + REG_SZ.length()).trim();
}
// This will allow an exception to be caught and saved as null
catch (Exception e)
{
return null;
}
}
//This creates a stream reader to read the values, it then places it in to a tempory string to be used
static class StreamReader extends Thread
{
private InputStream is;
private StringWriter sw;
StreamReader(InputStream is)
{
this.is = is;
sw = new StringWriter();
}
public void run()
{
try
{
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e)
{
;
}
}
// This puts the data found in the tempory string in to a fixed string
String getResult()
{
return sw.toString();
}
}
public static void main(String s[])
{
// This is what outputs the data in to a message display box
String Line1 = "Name of Device " + "Device Driver " + "Type of Device " + "\n";
String Line2 = getDeviceFriendlyName() + " " + getDeviceDriver() + " " + getDeviceClass();
JOptionPane.showMessageDialog (null, Line1 + Line2, "USB Devices",JOptionPane.PLAIN_MESSAGE);
}
}
Re: Whats wrong with my code??
One problem is the code is ignoring exceptions. Add a call to the printStackTrace() method to the catch blocks.