Sunday, January 30, 2011

Send SMS using Java

In most of the forums i found this question.How to send SMS Using java.
For answering this question i spent few hours for you guys.

For sending SMS you need to have the following files
2) comm.jar
3) win32com.dll
In this Link you can download the files.Let me know if the given link is broken.So that i will upload the files and change the link here.

Check the java_home twice before copy and paste the files.Because most of the problems occurred by pasting the files in wrong folder.
Should falls under
Should falls under
 Should falls under

Okay i believe you placed all files in respective folder correctly.
Wait guys i heard your mind voice,you are asking where is the java program.
How i will forget,here you go for java programs

import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class SriramSMS {
public void sendsms()   
 static Enumeration portList;
 static CommPortIdentifier portId;
 static SerialPort serialPort;
 static OutputStream outputStream;
 String messageString = "";
 String userName="";
 String phoneNumber="";
 Thread thread;

     userName = "";
     phoneNumber = "9876543210";
     messageString = " Hi." +userName+"welcome to this blog" ;

            String line1 = "AT+CMGF=1\r\n";
     String line2 = "AT+CMGS=" + phoneNumber + "\r\n"+messageString+ "\r\n";
     String line3 = "\u001A";

    // Here initialize the driver class
                  String driverName = "com.sun.comm.Win32Driver";
    CommDriver commdriver = (CommDriver)Class.forName(driverName).newInstance();

    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements())
      portId = (CommPortIdentifier) portList.nextElement();
      if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)   {
      System.out.println("SMS Sending........" + portId.getName());
      if((portId.getName().equals("COM1"))) {
         serialPort = (SerialPort)"SimpleWriteApp",10000);
         System.out.println("sms sending port--->"+serialPort);
         outputStream = serialPort.getOutputStream();
        } catch (PortInUseException portUse) {
         System.out.println("Port In Use " + portUse.toString());
        } catch (UnsupportedCommOperationException unsuppComOperExcep) {
         System.out.println("UnsupportedCommOperationException occured:::"+unsuppComOperExcep.toString());
        }  catch (IOException ioExcep) {
         System.out.println("Error occured while writing to output stream IOException" + ioExcep.toString());
        } catch(Exception excep) {
         System.out.println("Error writing message  with exception while closing " +excep.toString());
  } catch(Exception excep) {
   System.out.println("EXCEPTION raised while writing message@@" +excep.toString());
You got the program for sending sms through java program.

Want to Send Mail Using java program then click this Link and for more java program click this Link.
Do post your comments.

Saturday, January 29, 2011

Interview Questions

Like you guys i also surfed and prepared for the interview.Few months before got selected in one MNC Company.I can help you guys by providing set of interview questions.

By clicking this Link you can download the interview questions in rar file format.

why you are waiting just download the files and rock the interview.

All the best guys...

Thursday, January 27, 2011

Struts2 Select Box Error.

I believe you faced this error thats why you are here for the solution.
Instead of wasting our time straightly we will go to the solution.


package com.demo.actions;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class SelectList extends ActionSupport{
private ArrayList dayList;
public String execute() throws Exception {
//instantiate the Arraylist here
dayList = new ArrayList();
return SUCCESS;
//Make sure you have created the getter and setter method for ArrayList
public ArrayList getDayList() {
  return dayList;
public void setDayList(ArrayList dayList) {
  this.dayList= dayList;
Thats it your code will work like a charm.
Do post your comments and for more java programming click this Link.

Wednesday, January 26, 2011

Maintain Image Aspect Ratio Using Java

In Internet i have searched more about maintaining the aspect ratio of the image using java program.After a long struggle found one example and i changed according to met my requirement.Here you go for the complete solution.
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

class imageAspectRatio {
  public static void main(String args[]) {
  File file = new File("path");//Specify the input image source file location.
  try {
   FileInputStream fis = new FileInputStream(file);
   InputStream bis = new BufferedInputStream(fis);
   FileOutputStream fos = null;
   Image image = (Image);
   int thumbWidth = 100;//Specify image width in px
   int thumbHeight = 100;//Specify image height in px
   int imageWidth = image.getWidth(null);//get image Widht
   int imageHeight = image.getHeight(null);//get image Height 
   double thumbRatio = (double)thumbWidth/(double)thumbHeight;
   double imageRatio = (double)imageWidth/(double)imageHeight;
//This calculation is used to convert the image size according to the pixels mentioned above
   if(thumbRatio<imageRatio) {
    thumbHeight = (int) (thumbWidth/imageRatio);
   } else {
    thumbWidth = (int) (thumbHeight*imageRatio);
   BufferedImage thumbImage = 
       new BufferedImage(thumbWidth,thumbHeight,BufferedImage.TYPE_INT_RGB);
   Graphics2D graphics = thumbImage.createGraphics();
   graphics.drawImage(image,0, 0, thumbWidth, thumbHeight,null);
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam param  = encoder.getDefaultJPEGEncodeParam(thumbImage);
   int quality = 100;
   quality = Math.max(0,Math.min(quality,100));
//output image type.
   String format = "jpg";
   ImageIO.write(thumbImage,format, new File("path"));//Specify the location for output image .
    } catch(IOException ioExcep) {
  }catch(Exception excep) {

Run this program you will get the expected result.
Do post your comments and for more java programming click this Link.