Sunday, June 19, 2011

Creating Stateless EJB with GlassFish

In this sample example we will be creating the stateless ejb with glasshfish and ant. Using ant is optional as it will automate the build on basis of the script.


  1. GlassFish version 3.1 [i am using zip format]. Click to download
  2. Apache-ant version 1.8.2 [i am using .tar.gz format ]. Click to download
  3. Java up and running [ JDK ]
Note: For path classpath on windows use ; to append and on nix use :
We won't go in setting the path for anything. We will be using the bin folder to run. But yes for easy build, path can be set. The path can be set for glassfish and ant in following ways.
  1. For GlassFish : GLASSFISH_HOME=<absolute path of glassfish>/bin
  2. For ant : ANT_HOME=<absolutepath of ant >/bin
As we will go without path so we need to shift to bin folder to GlassFish extracted folder.

terminal screen to run glassfish

To test administrative console open the browser with url http://localhost:4848. We will get

Bingo! We are done for making the sample application.

Lets create HelloWorld Application. We need three java file,, and one xml file i.e. build.xml. We will create the package com.asb. Our directory structure will look like

This structure can exist in any drive or directory.

Code for the files

package com.asb;
public interface Hello
    public String sayHello();
    public String sayHelloRemote();


package com.asb;
public interface HelloLocal
    public String sayHello();
    public String sayHelloLocal();

package com.asb;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;

public class HelloBean implements Hello,HelloLocal
    public String sayHello()
        return "Hello World";
    public String sayHelloLocal()
        return "Hello Local World";
    public String sayHelloRemote()
       return "Hello Remote World";

<project name="HelloEJB" basedir=".">
         Simple Build File
    <!-- set global properties for this build -->
    <property name="src.dir" location="src"/>
    <property name="build.dir" location="build"/>
    <property name="dist.dir" location="dist"/>

    <target name="init">
        <!-- For timeStamp -->
        <mkdir dir="${build.dir}"/>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="true"/>

    <target name="dist" depends="compile">
        <mkdir dir="${dist.dir}/lib"/>
        <jar jarfile="${dist.dir}/lib/HelloBean-${DSTAMP}.jar" basedir="${build.dir}"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>

To build using ant we will go to bin directory where the ant was extracted.

ant/bin>./ant compile -buildfile <path to helloapp>/HelloEJB/build.xml -lib <path to glassfish>/lib/javaee.jar

./ant dist -buildfile <path to helloapp>/HelloEJB/build.xml -lib <path to glassfish>/lib/javaee.jar

glassfish/bin>./asadmin start-domain
glassfish/bin>./asadmin deploy <path to="" helloapp>/HelloEJB/dist/lib/HelloBean-<date>.jar
We are done with the deployment.

To run we need client program
package com.asb.client;

import javax.naming.*;
import com.asb.Hello;

public class HelloClient
    public static void main(String args[])
            InitialContext ctx = new InitialContext();
            Hello h = (Hello) ctx.lookup("com.asb.Hello");
        catch(Exception e)

To compile and run

javac -cp <path to glassfish>/lib/javaee.jar:. HelloClient
java -cp <path to glassfish>/lib/javaee.jar:<path to glassfish>/lib/appserv-rt.jar HelloClient

We are done we will get the output HelloWorld.

Friday, June 10, 2011

Java and MSAccess

A simple video on how to connect MSAcess and Java.

Creating Hello World Servlet

A simple video [ screencast ] to show how to develop the HelloWorld Servlet.