Wednesday, September 21, 2011

Create a Executable jar file from the command line or ANT build, Build Simple ANT File

Here i am posting how to make executable jar file from the command line as well through ANT tool, then how to run that executable jar file from the command prompt

This is the simple way to create executable jar file.

Step 1 : create folder inside your respective directory [Ex: c:/simple_java/src>]
Step 2 : create java file/files under this project[Package name is optional]
Ex: ,
under com.siva pacakage
Ex: c:/simple_java/src/com/siva>
compile : c:/simple_java/src>javac com/siva/*.java

Step 3 : create one MANIFEST.MF or manifest.txt
under src folder.[this file can be created any place in your computer]

Ex: c:/>simple_java/src/manifest.txt

We need to mention the main class details.

Manifest-Version: 1.0
Main-Class: com.siva.HelloWorld

Step 4 : Now we need to create the executable jar file with the following command

c:/simple_java/src>jar cvfm simple_java.jar manifest.txt com/siva/*.class

Step 5: Run the executable jar file

c:/simple_java/src>java -jar simple_java.jar

Simple ANT build.xml- which used to compile , run and create the executable jar file which includes manifest file.

Before Starting with build.xml- We need to know What is ANT(Another Neat Tool]. Used for to build the application.

setting the ANT in our system.

Step 1: Down load the latest code from the ANT site .[Prefer to download the .ZIP file]


Step 2: Unzip the Downloaded file, Then set the path.
Setting the Path
Right Click on My Computer ->Properties ->Advanced-> Environment Variables-> System variables -> select Path -> Edit ->give semicolon(;) end of the path value-> paste the ant location upto bin


Step 4: Create java programs related to your purpose.
Step 5: create build.xml file, then write the code inside build.xml, for
compilation, creating executable jar file the application.

Step 6: Run the build.xml-> by using the ant command


Down Load Source Code

Friday, September 16, 2011

Getting Started With DWR

This post contains the information about the DWR usage.
DWR- Direct Web Remoting is a java library that enables java on the server and JavaScript in a
browser to interact and call evch other as simply as possible.

DWR is Easy Ajax for Java.
DWR is a concept to implement Ajax based applications in java side.
I am posting the simple Dynamic Address Entry example. But you can modify this according to your requirement like fetching the data from data base, and assign that values to the respective inuput values. Here i am writing with simple hard coded data.

Follow below steps to learn DWR in minutes.
Step 1 : Start the eclipse- File -New- Dynamic web project- [provide project name] eg: dwr-project
Step 2: Open web.xml which is there in inside WEB-INF. Add the dwr related classes configuration.

<?xml version="1.0" encoding="UTF-8"?><web-app id="dwr" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=""&gt;



Step 3: create one foldercalled address inside WebContent
Step 4: create index.html inside address folder which we created in Step 3.
<html xmlns=""><head&gt;
<title>Dynamic Address Entry Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<script type='text/javascript' src='../dwr/engine.js'>
</script> <script type='text/javascript' src='../dwr/util.js'>
<script type='text/javascript' src='../dwr/interface/AddressLookup.js'> </script>
<script type="text/javascript" src='index.js'> </script>
<h1>Dynamic Address Entry With DWR</h1>
<p>This is a practical example of making form fill easier for users, and howsimple this is with

<div id="tabContents">
<div id="demoDiv">
<p>We can enter the below Post codes in the post code text box, and check the address fill up in the respective text boxes. In the same way we can save the details in data base and call the respective function.</p>

<table class="plain">

<input id="postcode" type="text" onkeyup="fillAddress()" onchange="fillAddress()"
<td>House name/number:</td>
<td><input id="house" type="text"/></td>
</tr> <tr>
<td>Line 2:</td> <td><input id="line2" type="text"/></td>
</tr> <tr>
<td>Line 3:</td> <td><input id="line3" type="text"/></td> </tr> <tr> <td>Line 4:</td> <td><input id="line4" type="text" size="30"/></td>
</tr> </table>
<p> </p> <p> </p>

In the index.html the bold lines are important. we no need to create AddressLookup.js, but we have to mention this. This should be same like a java class name.
Step 5: create index.js under address folder. this index.js is used to get the deatils from java and append the details in html page with out page refreshing.

function fillAddress() {
var postcode = dwr.util.getValue("postcode");
AddressLookup.fillAddress(postcode, function(address) {

dwr.util.setValue("line2", address.line2);
dwr.util.setValue("line3", address.line3);
dwr.util.setValue("line4", address.line4);

Step 6: Create one AddressLookup java class under src folder ,
This class is having all the data. It might be database data or any type of data.
Here i am mentioning Hard coded data.

package com.siva;
import java.util.HashMap;
import java.util.Map;
import org.directwebremoting.util.LocalUtil;

public class AddressLookup
private static final String LINE4 = "line4";
private static final String LINE3 = "line3";
private static final String LINE2 = "line2";
private static final String HOUSE_NO = "house";
* @param origpostcode the code to lookup
* @return a map of postcode data
public Map fillAddress(String origpostcode)
Map reply = new HashMap();
String postcode = LocalUtil.replace(origpostcode, " ", "");
if (postcode.equalsIgnoreCase("600041"))
reply.put(HOUSE_NO, "14/1 FLAT NO- G3");
reply.put(LINE2, "Kalakhestra Road");
reply.put(LINE3, "TiruvanMiyur");
reply.put(LINE4, "Chennai");
else if (postcode.equalsIgnoreCase("400708"))
reply.put(HOUSE_NO, "Door NO 201");
reply.put(LINE2, "Sakinaka Road");
reply.put(LINE3, "Airoli");
reply.put(LINE4, "Navi Mumbai");
else if (postcode.equalsIgnoreCase("400707"))
reply.put(HOUSE_NO, "Door No 202");
reply.put(LINE2, "Mullund Road");
reply.put(LINE3, "Vashi");
reply.put(LINE4, "Navi Mumbai");
else if (postcode.equalsIgnoreCase("508517"))
reply.put(HOUSE_NO, "Door No 345");
reply.put(LINE2, "Bangalore Road");
reply.put(LINE3, "Balaji Colony ");
reply.put(LINE4, "Tirupathi");
else if (postcode.equalsIgnoreCase("516269"))
reply.put(HOUSE_NO, "Door No 234");
reply.put(LINE2, "Bangalore Road");
reply.put(LINE3, "Kothapeta");
reply.put(LINE4, "Rayachoty");
reply.put(LINE2, "Postcode not found");
reply.put(LINE3, "");
reply.put(LINE4, "");
return reply;

Step 7: create dwr.xml inside WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" ""&gt;
<create creator="new" javascript="AddressLookup">
<param name="class" value="com.siva.AddressLookup"/>

Step 8 : Add below jars in the lib folder

Step 9: Right click on the project and Run As Server- After successful run open the browser.
enter the post code which mentioned in the index.html.
Automatically the data has beed in filled in the remaining text boxes. in this way DWR work. you can explore more things on the DWR (
Address bar has to be given in the following way
Step 10 : your DWR application started.

Download the source code for this project from here.


After downloading Unzip this and import into eclipse, and run project - Run as Server.

Wednesday, September 14, 2011

Spring MVC MultiActionController - simple login application

Hello All,

Here i am posting simple Spring MVC example with IOC(setter , Constructor Injection).

Follow the steps to execute the program in your eclipse.

Step1 : Open Eclipse - Create new Dynamic Web Project

Step2 : After creating Project, open web.xml which is there inside created project.

edit Web.xml and add the below code.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">




Step 3: As per above web.xml code- the servlet name as springweb so As per spring mvc we have to create xml file called springweb-servlet.xml under WEB-INF

Step 4: Inside springweb-servlet.xml , we have to put configuration details for the controller's, jsp pages., IOC information. as mentioned below.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xsi:schemaLocation=";

<!-- no 'id' required, HandlerMapping beans are automatically detected by the DispatcherServlet -->

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">


<prop key="login.htm">loginController</prop>

<prop key="success.htm">loginController</prop>




<bean id="loginController" class="com.siva.controller.LoginController">

<property name="name" value="siva"/>

<property name="age" value="27"/>

<constructor-arg type="int" value="27"/>

<constructor-arg type="java.lang.String" value="raju"/>


<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>



Step 5: Create inside src folder- under any package

package com.siva.form;
public class LoginForm implements Serializable {

private static final long serialVersionUID = 1L;
private String userName;
private String password;
public String getUserName() {
return userName;
public void setUserName(String userName) {
this.userName = userName;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;

Step 6: Create LoginController which extends MultiActionController,

package com.siva.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.siva.form.LoginForm;

public class LoginController extends MultiActionController {

public LoginForm loginForm;
private String name;
private String age;

public LoginController(){

//This is constructor type setting in IOC - Check on springweb-servlet.xml for further reference.
//If you are still confusing the Study IOC basics.

public LoginController (String name1 , int age1)
System.out.println("Name------" + name1);
System.out.println("Age------" + age1);

* This method has been called when ever the user enter login.htm under project.
* In this GetName, getAge methods are one way of IOC through set methods. We set the values
* in springweb-servlet.xml as a property and value. So when ever we called respective get methods
* we will get the results from the springweb-servlet.xml.

public ModelAndView login(HttpServletRequest request, HttpServletResponse response, LoginForm form) {

ModelAndView mav = new ModelAndView();
System.out.println("Name for the set IOC " + getName());
System.out.println("Age for the set IOC " + getAge());
return mav;

* This method also will after enter the username and password in login page.
* It will check whether user name is 'siva' and password is 'raju'. if it's true then
* it will redirect to the success page. otherwise it will remain in the same page

public ModelAndView success(HttpServletRequest request, HttpServletResponse response, LoginForm form){
ModelAndView mav = new ModelAndView();
//User Enter name and password
System.out.println("UserName----" + form.getUserName());
System.out.println("Password-----" + form.getPassword());

// Injected name and age through IOC
System.out.println("Name for the set IOC " + getName());
System.out.println("Age for the set IOC " + getAge());
if(form.getUserName() != null && form.getUserName().equalsIgnoreCase("siva") &&
form.getPassword() != null && form.getPassword().equalsIgnoreCase("raju")) {
return mav;
public String getName() {
return name;

public void setName(String name) { = name;

public String getAge() {
return age;

public void setAge(String age) {
this.age = age;

Step 7 : Create login.jsp, success.jsp . Place these 2 jsp pages under WEB-INF/jsp/


<%@ taglib uri="" prefix="form" %>

<form:form commandName="loginForm" id="loginForm"

action="success.htm" method="POST" >

Username <form:input path="userName"/> </br>

Password <form:input path="password"/></br> <

input type="submit" value="Login"/>






Step 8: Add required jar's
















all the above jar's can be used to run the application.

Step 9: Now right click on the project- Run As - Run on Server-

select the appropriate server, and run the application.

Step 10: These are the steps to run simple MVC MultiActionController example.

Get the source code from this link

Wednesday, September 7, 2011

Getting Started With Spring batch 2.0

Hello world example using spring batch 2.0

Hello world example with spring batch 2.0 along with eclipse.
Follow below steps to start the hello world example with spring batch.

Step 1 : open eclipse - create new java project.
Step 2 : create new xml file under project (applicationContext.xml)
Step 3: create one more xml. under project location.(simpleJob.xml)
Step 4: create one java class which implements
Step 5: override the required methods in your respective class.
Step 6: we can run the job through
This class will take two parameters (Job path and job parameters).
We need to pass those details.
Step 7: Here i am posting, how to run through eclipse.
Right click on the java class( Which we written)- Select RunConfigurations -
Select Main tab- Select Main class as
Click on Arguments - Enter Jobpath and JobParameters under Program arguments
Eg: simpleJob.xml helloWorldJob
Click on the Run Button then hello world will display on the console.

Step 8: Required jar files

In this way you can start the Spring batch.

Source Code of applicationContext.xml


Source Code of simpleJob.xml

Source Code of
package com.siva;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class HelloWorld implements Tasklet {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
System.out.println("Hello World");
return RepeatStatus.FINISHED;
Source Code of the

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c{1} %x - %m%n


Contact Form


Email *

Message *