Sunday, December 27, 2009

Windows Hard Drive Serial Number

public static String getSerialNumber(String drive) {
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);

String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
+"Set colDrives = objFSO.Drives\n"
+"Set objDrive = colDrives.item(\"" + drive + "\")\n"
+"Wscript.Echo objDrive.SerialNumber"; // see note
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();
}

Execute an OS Command

public static void main (String [] args) throws IOException {

String [] Command = null;

if (System.getProperty("os.name").equals("Linux")) {
Command = new String[1];
Command[0] = "df";
}
if (System.getProperty("os.name").equals("Solaris")) {
Command = new String[2];
Command[0] = "df";
Command[1] = "-k";
}
if (Command == null) {
System.out.println("Can't find free space on this OS");
System.exit(1);
}

Process Findspace = Runtime.getRuntime().exec(Command);

BufferedReader Resultset = new BufferedReader(
new InputStreamReader (
Findspace.getInputStream()));

String line;
while ((line = Resultset.readLine()) != null) {
System.out.println(line);
}
}
}

Saturday, December 26, 2009

Simple FTP Client

private void download( String ftpServer, String user, String password,
String fileName, File destination ) throws MalformedURLException,
IOException
{
if (ftpServer != null && fileName != null && destination != null)
{
StringBuffer sb = new StringBuffer( "ftp://" );
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append( user );
sb.append( ':' );
sb.append( password );
sb.append( '@' );
}
sb.append( ftpServer );
sb.append( '/' );
sb.append( fileName );
/*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing
*/
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();

bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new FileOutputStream(
destination.getName() ) );

int i;
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( "Input not available" );
}
}

Thursday, December 24, 2009

Setting JAVA_HOME in RHL5

Edit /etc/profile.


JAVA_HOME=/usr/java/jre1.6.0_17
export JAVA_HOME
PATH=$ORACLE_HOME/bin:$JAVA_HOME/bin:$PATH
export PATH

Saturday, December 19, 2009

Make your current web page look disabled.

If you need to make your current web page look disabled, in order to display a form or message box, simply add the following div onto your page,
and make sure you give it the following css properties as well.

div:

div id="somediv" class="opaqueDiv"


css:

.opaqueDiv{
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
filter: alpha(opacity=30);
position:fixed;
top:0px;left:0px;
background:white;
width:100%;
height:100%;


}