Class loading and java reflection

Here is some simple example of class loading and java reflection.

package com.sun;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.sun1.TestClass;
public class MainClass {
public static void main(String[] args) {
System.out.println(“Testing classloading…”);
Class clazz = Class.forName(“com.sun1.TestClass”);
Method [] methods = clazz.getDeclaredMethods();
TestClass tc = new TestClass();
for (Method method : methods) {
method.invoke(tc, new Object[]{“Sunil Chauraha”});
}catch(InvocationTargetException e){
}catch (IllegalAccessException e) {
}catch(ClassNotFoundException cnf){
System.err.println(cnf.getMessage()+” : “+cnf);

package com.sun1;
public class TestClass {
public void pring(String value){
System.out.println(“The value passed is : “+value);


UML Diagram and Eclipse


Here are the links for uml plug in configuration with eclipse.

Global warming

When the word “Global warming” comes into mind or on voice or in ears. I start thinking who is responsible for this ever ending problem? I think, in fact i am sure that me, you and they all are responsible for this outcome. As a human being what we are doing or what we have done or what we are going to do. Some where that follows Einstein’s E=MC2 and produces unnecessary energy and the global warming increases by 0.000000001 % or more.

My dear friend please think something outstanding or wonderful thing that can decrease the rate of increase in temperature……… Continue reading

Singleton design pattern

As the name says… Singleton -> means just single.

There are so many situations where we need to create many or one instance of our class.  Like we will have a single database connection manger.

There are many ways to create a singleton design (i.e. Using User Defined exception class, using static class).

rather there is the most useful way that most programmer uses.  That is static method having a private default constructor.

public class IsSingle {
static boolean instanceFlag = false;
private IsSingle(){}
public static IsSingle getInstance(){
instanceFlag = true;
return new IsSingle();
return null;
// finalize is needed here, because if object IsSingle is garbage collected then isInstance must be false to invoke new.
public void finalize(){
instanceFlag = false;

Reading a Doc file using Java

Hi by using a simple java IO we can’t read contents of a doc file.

Jakarta poi has some utilities to read contents of a file using its api.

Every line of the doc file is considered as a paragraph.

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadandWrite {
public static void readDocFile(String filePath) {
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem(new FileInputStream(filePath));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
String[] paragraphs = we.getParagraphText();
System.out.println(“Word Document has ” + paragraphs.length+ ” paragraphs”);
for (int i = 0; i < paragraphs.length; i++) {
paragraphs[i] = paragraphs[i].replaceAll(“\\cM?”, “”);
} catch (Exception e) {
public static void main(String[] args) throws Throwable {

For this i used  poi-3.0-FINAL.jar.

How to use Gmail as your SMTP server – JAVA

Hi Here is an interesting code to send an email using gmail smtp server.

This asks for the login and password of any gmail accound and uses gmail smtp to send emails…


package com.sunil.mail;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class SimpleJavaMail {

private Session mailSession;
protected PasswordAuthentication authentication;
private String user = null;
private String password = null;
private Properties props;

public SimpleJavaMail(){
props = new Properties();
props.setProperty(“mail.transport.protocol”, “smtp”);
props.setProperty(“”, “”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.port”, “465”);
props.put(“mail.smtp.socketFactory.port”, “465”);
props.put(“mail.smtp.socketFactory.class”, “”);
props.put(“mail.smtp.socketFactory.fallback”, “false”);
props.put(“mail.user”, “”);
props.put(“mail.password”, “xxxxx”);
props.setProperty(“mail.smtp.quitwait”, “false”);

public void initMail(String user, String password) {
this.user = user;
this.password = password;
mailSession = createMailSession();

private Session createMailSession() {
Session session = null;
try {
session = Session.getDefaultInstance(props,    new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
} catch (Exception e) {
return session;

public synchronized void sendMail(String subject, String body, String sender, String recipients)
throws Exception{
String host;
String mailUserName = (String) getProps().get(“mail.user”);
String mailPassword = (String) getProps().get(“mail.password”);
host = (String) getProps().get(“”);
host = host.toLowerCase();
initMail(mailUserName, mailPassword);
MimeMessage message = new MimeMessage(mailSession);
message.setSender(new InternetAddress(sender));
message.setContent(body, “text/plain”);
if (recipients.indexOf(‘,’) > 0){
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

public Properties getProps() {
return props;

public void setProps(Properties props) {
this.props = props;

public static void main(String[] args) {
String subject = “Test mail…”;
String body = “Hi Buddy…”;
String sender = “”;
String recipients = “”;
SimpleJavaMail mailJava = new SimpleJavaMail();
mailJava.sendMail(subject, body, sender, recipients);
} catch (Exception e) {
System.out.println(“Mail sent!”);


Running DOS command using java!


Here is a small example.

We can use to run a dos command using java code.

/***************Code ******************/


class SunilCmd {

static public void main(String args[]) {
try {
StringBuffer sb = new StringBuffer();
BufferedReader buff = new BufferedReader(new InputStreamReader(    Runtime.getRuntime().exec(“cmd.exe /C dir”).getInputStream()));
/* Here Runtime.getRuntime creates environment to run the command */
/* .exec executes the parameters given inside the method */

* here “cmd.exe /C” will invoke the DOS in exclusive mode and “dir”
* is a command that will be executed…..

String line = buff.readLine();
while (line != null) {
sb.append(line + “\n”);
line = buff.readLine();
File f = new File(“output.txt”);
*All the output will be written into output.txt file
FileOutputStream fos = new FileOutputStream(f);
} catch (Exception ex) {