At the time of this writing (Aug 2006), Glassfish has the most complete implementation of Java EE 5, including EJB 3. This tutorial shows how to install Glassfish from scratch and then develop and test a simple Session EJB using Eclipse. This is meant for developers who will like to learn EJB 3 right now, before any commercial development IDE becomes available.

Download Glassfish

First download Glassfish from Here. Download a milestone build for maximum stability. This tutorial was developed using V1 Milestone 7. It is recommended that to follow this tutorial you use a V1 build rather than V2 (which may behave slightly differently).

Glassfish will be downloaded as a JAR file (such as glassfish-installer-9.0-b48.jar).

Install Glassfish

First install Sun JDK 1.5 (also called J2SE 5). Installation of this is very simple and beyond the scope of this article.

Open a command window.

Set the JAVA_HOME variable to point to the root installation folder of JDK. For example:

set JAVA_HOME=c:jdk15

Copy the Glassfish JAR file to C:.

Begin installation of Glassfish, by entering the following command from C:.

java -Xmx256M -jar glassfish-installer-_XXXX_.jar

System will open a license window.

Development Glassfish

Scroll down and click on Accept.

System will extract all the files in C:glassfish.

In the command window, change directory to that folder.

cd c:glassfish

To complete the setup, run this command:

libantbinant -f setup.xml

Make sure that the command ends with a BUILD SUCCESSFUL message. Congratulations, the installation is now complete.

Start the Servers

First, we will start the Derby database server. This is really not necessary for this tutorial. But, it is a good idea to run the database if you will do more advanced EJB development (such as timers and entity persistence).

In the command prompt, change directory to C:glassfishbin folder. Then, enter the command:

asadmin start-database

After the database starts, run the application server.

asadmin start-domain

We will verify the installation by logging into the administration console. Open a new browser window and enter the URL http://localhost:4848/asadmin.

Login using the user ID admin and password adminadmin. This will validate the installation.

Create Projects in Eclipse

We will assume that you already have a fully functional Eclipse 3.2 installation. Launch Eclipse. We will create two Java projects:

  1. For the EJB module
  2. For the EJB client

First, create a new Java project called Simple EJB Project.

Now, we will add two JAR files from Glassfish to the compiler’s classpath. Open the properties dialog of the project. Then select the Java Build Path property. Click on the Libraries tab. Click on Add External JARs.

Navigate to the C:glassfishlib folder and select appserv-rt.jar and javaee.jar. Click on Open.

Make sure that the two JAR files are added to the compiler’s class path. Click on OK to close the properties dialog. Note: You can export this Java project as an archive file and easily import it later for quickly creating a new Glassfish EJB project.

Now, we will create the client project called Simple Client Project. Quickest way to create this project is to copy the Simple EJB Project and paste it as Simple Client Project. Alternatively, you can create a new Java project and add the two JAR files to the build path as shown above.

The client project needs to refer to the remote or local interfaces of the EJBs. The simplest way to set this up is to set a dependency between the client and EJB projects. In real life, the client may be developed by a different team than the EJB and the client developers may not have access to the EJB project. In this case, the EJB developers need to export a client JAR file and hand it to the client developers. We will keep things simple, and have the client project refer to the EJB project.

Open the Properties dialog of the Simple Client Project. Select the Java Build Pathproperty. Click on the Projects tab. Click on Add. Select the check box next to Simple EJB Project. Click on OK.

Click on OK to accept the changes.

Write a Simple Session EJB

In the Simple EJB Project, create a new package called com.webage.ejbs. First, we will create the remote interface for the EJB. In the package you have just created, create a new Java interface called SimpleBean. Add the following code:

import javax.ejb.*;

public interface SimpleBean {
 public String sayHello(String name);

Note: The @Remote annotation marks this interface as a remote interface. This annotation belongs to the javax.ejb package. Hence, we had imported the package in the file. Alternatively, you could use the annotation @javax.ejb.Remote.

Save and close this file.

Now, we will create the bean class. In the same package, create a new Java class called SimpleBeanImpl. Add the following code.

import javax.ejb.*;

@Stateless(name="Example", mappedName="ejb/SimpleBeanJNDI")
public class SimpleBeanImpl implements SimpleBean {
 public String sayHello(String name) {
 return "Hello " + name + "!";


  1. The @Stateless annotation (available again from the javax.ejb package) marks this class a stateless session EJB class.
  2. The name of the bean is “Example”. This name could be used to provide additional configuration information about the EJB in various deployment descriptors (such as ejb-jar.xml or vendor specific sun-ejb-jar.xml). We are not going to do that for now.
  3. The mappedName is the global JNDI name of the EJB. This is set to “ejb/SimpleBeanJNDI”.

Save and close this file.

Develop the Client

In the Simple Client Project, create a new package called com.webage.client. In this package, create a new class called TestClient. Add the following code:

import javax.naming.*;
import com.webage.ejbs.SimpleBean;

public class TestClient {

 public void runTest() throws Exception {
 InitialContext ctx = new InitialContext();
 SimpleBean bean = (SimpleBean) ctx.lookup("ejb/SimpleBeanJNDI");
 String result = bean.sayHello("Billy Bob");

 public static void main(String[] args) {
 try {
 TestClient cli = new TestClient();

 } catch (Exception e) {

Note: We do a JNDI lookup of the name “ejb/SimpleBeanJNDI” as this has been configured as the JNDI name of the EJB. We can not use the dependency injection annotation @EJB to do the look up as our client will run outside of any Java EE container.

The rest of the code should be fairly straight forward. Save and close this file.

Deploy the EJB Module

We will use the automatic deployment feature of Glassfish to rapidly deploy the EJB module. This option involves, simply dropping the EJB JAR file under the C:glassfishdomainsdomain1autodeploy folder.

First, we will export the EJB JAR file. Right click on Simple EJB Project and select Export.

Expand Java and select JAR file. Click on Next.

In the JAR file text box, enter C:glassfishdomainsdomain1autodeploytest_ejb.jar.

Also, check the Overwrite existing files without warning option. This will speed up the export process in subsequent times.

Click on Finish to export the JAR file.

Glassfish will automatically install the EJB JAR file within a few seconds. It is a good idea to monitor the server’s log file to be certain if the EJB JAR file was deployed successfully. Monitoring the log file will also help you detect problems with your EJB code as they occur at runtime. The log file is located at C:glassfishdomainsdomain1logsserver.log. You can use a tool like PigTail to monitor the file.

Run the Client

Switch back to Eclipse.

Development for Glassfish

In the Package Explorer view, right click on and select Run As->Java Application. Make sure that the Console view shows the following output.

Re-deploying EJB

During development, you will no doubt change the EJB code frequently. To re-deploy the EJB JAR file, simply export the EJB JAR file again following the process already mentioned.

Shutdown Servers

From the C:glassfishbin folder, run these commands:

asadmin stop-database

asadmin stop-domain


In this tutorial, we have set up a development environment for Glassfish based EJB 3 development. We developed and tested a stateless session EJB. You can use this environment and approach to develop more complex EJBs, including entity persistence.