Enterprise JavaBeans: Getting Started with EJB, Part 6

Tuesday Feb 13th 2001 by Richard G. Baldwin

Baldwin shows you how to use the New Enterprise Bean Wizard to deploy your J2EE application into the J2EE server.

Java Programming, Lecture Notes #1315


This is one in a series of lessons designed to teach you about Sun's Enterprise JavaBeans(TM) (EJB).

Sixth in a miniseries

This lesson is also the sixth lesson in a miniseries intended to get you up and running with Sun's Enterprise JavaBeans(TM) very quickly.


The following three steps are required to deploy your bean into the J2EE server:
  1. Create the J2EE Application
  2. Package the Enterprise Bean
  3. Deploy the J2EE Application
Previous lessons showed you how to accomplish the first two steps.  This lesson will show you how to accomplish the third step.  This will involve several operations, including:
  • Starting the J2EE server and the deployment tool
  • Specifying the JNDI name
  • Actually deploying the application
  • Creating a client jar file
  • Completing the deployment process
I will walk you through the operations necessary to accomplish this step without attempting to explain the purpose of each operation.  Subsequent lessons will provide information that bears on the purpose of the operations.


Writing, compiling, deploying and executing a simple bean

The lessons in this miniseries will show you the steps involved in writing, compiling, deploying and executing a simple bean.  The discussion will be brief, and is intended primarily to get you to the point that you can get something running very quickly.

More details in subsequent lessons

Subsequent lessons will come back and fill in the gaps to help you more-fully understand each of the steps.

Subsequent lessons will also teach you about the different kinds of beans and how to use those beans in different situations.

Recommended supplementary reading

It is strongly recommended that you study the previous lesson entitled Enterprise JavaBeans(TM), Getting Started with EJB, Part 5 before embarking on this lesson.

Additional supplementary material that I have written is available at Gamelan.  As of the date of this writing, Gamelan does not maintain a consolidated index to my Java tutorials.  Therefore, they may be difficult to locate on the Gamelan site.  A consolidated index with links to the tutorials on the Gamelan site is available at Baldwin's Java Programming Tutorials.

The required steps

An earlier lesson entitled Getting Started with EJB, Part 1 contained a list of the required steps for writing, compiling, deploying and executing a simple bean.  Abbreviated versions of the first seven steps in that list are repeated below.

  • Download and install Java 2 Platform, Standard Edition from Sun.
  • Download and install the Java 2 Platform, Enterprise Edition.
  • Write the bean class and required helper classes.
  • Write home and remote interfaces.
  • Write a compilation batch file for the bean and the interfaces.
  • Compile the bean class, helper classes, and interfaces.
  • Use Sun's J2EE deploytool program to deploy the bean into the server.
The last step is a big one

The last step shown above is a big one, so I have subdivided it into several sequential lessons, beginning with the lesson entitled Enterprise JavaBeans(TM), Getting Started with EJB, Part 3.

In this group of lessons associated with deployment, I am walking you through the detailed steps necessary to deploy your new bean in the J2EE server.  I am providing that information in cookbook fashion, meaning that I will tell you what to do but not necessarily why to do it.

Application and bean jar files are available

In this lesson, I am assuming that you have completed the first six steps and the first two parts of the seventh step.  The first two parts of the seventh step are described in the lessons entitled Enterprise JavaBeans(TM), Getting Started with EJB, Parts 4 and 5.

Having completed these steps, you should have a J2EE application file named HelloApp.ear that you created in a previous lesson entitled Enterprise JavaBeans(TM), Getting Started with EJB, Part 4.

The main topic of this lesson

That application should contain your Enterprise Bean packaged in a jar file (you packaged your bean in a jar file in Part 5).  However, the application hasn't yet been deployed into the J2EE server.  Deployment of the application into the J2EE server is the main topic of this lesson.

Viewing tip

This lesson provides screen shots of the critical steps in the process.  You may find it useful to open another copy of this lesson in a separate browser window.  That will make it easier for you to scroll back and forth and refer to earlier material and figures without losing your place.

Beans versus beans

Just in case you are reading this lesson without having read the earlier lessons in this series, I will tell you that an Enterprise Bean is not the same thing as a JavaBean Component(TM), as discussed in my series of tutorial lessons beginning with lesson number 500.

Sample Bean Program

At this point, you should have packaged the class files for your bean in the jar file that is your bean.  A complete listing of the bean program and the two required interfaces is provided near the end of this lesson.

Creating the J2EE Application

You created your J2EE application in a previous lesson and named the file containing that application HelloApp.ear.  In the lesson immediately prior to this one, you packaged the class files for your bean in a jar file along with a manifest file and a deployment descriptor file.  You put that jar file in the application file.

Starting the System

Assuming that you shut down the J2EE server and the deployment tool following the previous lesson, the first thing that you need to do is to open two Command Prompt windows.  I will refer to the Command Prompt windows simply as windows.

Start the J2EE server

To start the J2EE server running, enter the following command at the prompt in one of the windows:

j2ee -verbose

A large amount of text will appear on the screen.  Make certain that the text on that screen ends with the following text.  Otherwise, the server hasn't been properly started.

J2EE server startup complete.

Starting the deployment tool

Once the server has been started successfully, you need to start the deployment tool running.  To do this, enter the following command in another window:


A splash screen will appear first followed by a screen that looks similar to Figure 1.  This is the main deployment screen.  (Unfortunately, the text in this screen shot isn't readable.  This screen is quite large and I had to reduce the size of the screen shot considerably to make it fit in this narrow publication format.)

Figure 1. The J2EE Deployment Tool Main Screen.

My color coding scheme

Because the following text will be a mixture of action statements and explanatory text, I will color the action statements the following color of blue to make them easy to separate from the explanatory text.

Packaging the Enterprise Bean

You packaged your Enterprise Bean in a jar file and put that jar file in the application file in the previous lesson entitled Enterprise JavaBeans(TM):  Getting Started with EJB, Part 5.  There is nothing further that needs to be done in that regard.

Deploying the J2EE Application

Our new application now contains an Enterprise Bean and is ready for deployment into the J2EE server.  That is the primary topic of this lesson.

What is JNDI?

JNDI stands for Java Naming and Directory Interface(TM).

According to Sun:

"The Java Naming and Directory 
Interface(TM) (JNDI) is a standard 
extension to the Java(TM) platform, 
providing Java technology-enabled 
applications with a unified interface
to multiple naming and directory 
services in the enterprise. As part 
of the Java Enterprise API set, JNDI
enables seamless connectivity to 
heterogeneous enterprise naming and
directory services. Developers can 
now build powerful and portable 
directory-enabled applications 
using this industry standard." 

What does this mean?

That's a mouthful for sure.  Here is what it means in this situation.

Later, we will be building a Java client application that can communicate with the J2EE server and invoke business methods on our Enterprise Bean.  That application must be able to identify the bean upon which the business methods are to be invoked.  In this section, we will specify a name that will be used by the client to identify the bean.  That is the JNDI name.

The JNDI name

I will have more to say about the technical ramifications of JNDI naming in a future lesson.  At this time, we simply need to specify a name for the bean that is different from names given to other beans that have been deployed in the server.

Confirm the correct application

First you need to make certain that you are working on the correct application.

To do this, select HelloApp in the tree view of the screen shown in Figure 1.

Specify the JNDI name

The next step is to specify the JNDI name of the bean.

Select the JNDI Names tab.

This will cause the screen to change to look something like Figure 2.

Figure 2. JNDI Name Screen.

Type MyHello in the JNDI Names field and press the Enter key on your keyboard.

Deploy the application

The next step is to actually deploy the application.

To do this, select the Tools pulldown menu.

Then select Deploy Application from the Tools pulldown menu.

This will cause a screen to appear that looks something like Figure 3.

Figure 3. Screen Used to Specify Server to Deploy To.

Specify the J2EE server

On this screen, verify that the Target Server selection near the center of the screen is either "localhost" (or the name of the host running the J2EE server).  If you have been following the instructions in these lessons up to this point, it should be "localhost."

Check Return Client Jar

Next, you should select the checkbox entitled Return Client Jar.

This will cause the appearance of the screen to change to something like that shown in Figure 4 where an additional text field has been added at the bottom of the screen.

Figure 4. Modified Screen Showing Client Jar File Name.

Specify path for the client jar file

Enter the full path name for the file HelloAppClient.jar in the text field that appears near the bottom of the screen.

This jar file will be needed later when we build a client application that accesses the bean.

This path will specify where you would like for this jar file to reside.  For this example, you can simply accept the default path already in the field.

Get the JNDI Name Verification screen

Click Next to proceed to the next screen, which should look something like Figure 5.

Figure 5. JNDI Name Verification Screen.

On this screen, you should verify that the JNDI name for the HelloBean is MyHello.

Get the Completion screen

Click Next to proceed to a screen that looks like Figure 6.

Figure 6. Completion Screen.

This screen contains some information about the status of your efforts.  Assuming that the text reads "Click Finish to deploy the application,"

Click Finish on this screen.

The Deployment Progress screen

That should cause the application to be deployed and a deployment progress screen that looks something like Figure 7 should appear.

Figure 7. Final Deployment Progress Screen.

A lot of work is being done as these progress bars move up the screen.  If everything goes well, both the blue bar and the green bar should progress to the top with no error messages being generated.

Click OK when both bars reach the top, indicating that the deployment is complete.

Back to the J2EE Deployment Tool Main Screen

This will take you back to the J2EE Deployment Tool Main Screen shown in Figure 1.

Terminate the program

Choose Exit on the File menu of the screen shown in Figure 1 to terminate the Application Deployment Tool.

Enter j2ee -stop at the command prompt to terminate the J2EE server.

Your Enterprise Bean has been deployed

The new application and its associated Enterprise Bean have now been deployed in your J2EE server.  If you had a client program ready to run at this point, you could access the server and execute the business method of the bean.

I will show you how to write such a client program in the next lesson.

Your application file

At this point, you also have an application file named HelloApp.ear, which contains the following files:

  • meta-inf\manifest.mf
  • meta-inf\application.xml
  • meta-inf\sun-j2ee-ri.xml
  • ejb-jar-ic.jar
The manifest file is empty.

The application.xml file

The application.xml file contains the text shown in Listing 1.  Note that I have manually entered some line breaks to force this material to fit in this format.  (I also added boldface and color for emphasis.)

<?xml version="1.0" encoding="Cp1252"?>

<!DOCTYPE application 
PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN' 'http://java.sun.com/j2ee/dtds/

  <description>Application description

Listing 1

At this point, you can ignore everything near the top that I haven't highlighted in boldface.  (For more information on XML, see the consolidated index on my web site.)

The application element

The application element is highlighted in boldface.  I used color to help separate the three elements that are nested inside the application element.

The boldface material indicates that this is an application with the display-name and description shown and containing a single moduleI, which is an ejb jar file.

(Presumably, if I had also added web components in addition to the Enterprise Bean, additional modules would be identified in the XML file.)

The file named sun-j2ee-ri.xml

The contents of the file named sun-j2ee-ri.xml are shown in Listing 2.

<?xml version="1.0" encoding="Cp1252"?>

  <rolemapping />

Listing 2

The enterprise-beans element

This XML file contains information about things that we haven't discussed yet.  However, you will probably recognize the enterprise-beans element highlighted in boldface.

You will also probably recognize the red ejb-name and blue jndi-name elements nested inside the enterprise-beans element.

The Enterprise Bean jar file

The file named ejb-jar-ic.jar in the above list of files is the jar file containing the Enterprise Bean.  The following files are encapsulated in this jar file:

  • Hello.class
  • HelloBean.class
  • HelloHome.class
  • meta-inf\ejb-jar.xml
  • meta-inf\manifest.mf
You will probably recognize the first three of these files as the compiled class files for the program source code that is provided near the end of this lesson.

The manifest file

Again, the manifest file is essentially empty, except that it does contain the single line of text shown in listing 3.

Manifest-Version: 1.0

Listing 3

The deployment descriptor file

The contents of the deployment descriptor file named ejn-jar.xml are shown in Listing 4.  (Again, I manually entered some line breaks to force the material to fit in this narrow format.)

<?xml version="1.0" encoding="Cp1252"?>

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems,
nc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>

  <description>Hello Bean</description>
   <description>A hello bean</description>

Listing 4

This is the deployment descriptor file for the Enterprise Bean. (Once again, I have used color in an attempt to visually separate the elements in the XML document.)

Although you probably don't have the detailed knowledge at this point to understand everything in Listing 4, you will probably see some glimmer of recognition for all of the elements except perhaps the transaction-type element.  I will be discussing transaction processing and how that effects Enterprise Beans in future lessons.


The following three steps are required to deploy your bean into the J2EE server:
  1. Create the J2EE Application
  2. Package the Enterprise Bean
  3. Deploy the J2EE Application
Previous lessons showed you how to accomplish the first two of these steps.  This lesson showed you how to accomplish the third step.  This involved several operations, including:
  • Starting the J2EE server and the deployment tool
  • Learning a little about JNDI
  • Confirming the correct application
  • Specifying the JNDI name
  • Actually deploying the application
  • Creating a client jar file
  • Completing the deployment process
Once the application had been deployed, I showed and briefly discussed the contents of several of the files created by the deployment process.

Where To From Here?

In the next lesson, I will show you how to write a client program that executes the bean that you have installed in the J2EE server.

Complete Program Listings

A complete listing of the bean program is shown in Listing 5 below.  Complete listings of the remote and home interfaces are shown in Listing 6 and Listing 7.
/* File HelloBean.java
Copyright 2000 R.G.Baldwin
Rev 9/15/00

This bean receives and returns a String object
via a single business method.  It also displays
information about its life cycle in the 
System.out log file or on the system comsole
depending on whether or not System.out has been
directed to the log files.

This is a stateless session bean.  According to
Sun, a session bean must meet the following

Implement the SessionBean interface.
Class is defined as public.
Class cannot be defined as abstract or final.
Implements one or more ejbCreate methods.
Implements the business methods.
Contains a public constructor with no 
Must not define the finalize method.

Tested using Sun j2ee and JDK 1.3 under 
WinNT 4.0 workstation.
import javax.ejb.*;
import java.util.Date;

public class HelloBean implements SessionBean{
  SessionContext ctx;

  public void ejbCreate(){
    System.out.println("HelloBean Created " 
                                  + new Date());
  }//end ejbCreate()

  public String aBusinessMethod(String str){
    return "Hello " + str;
  }//end aBusinessMethod()

  public void ejbRemove(){
    System.out.println("HelloBean Removed " 
                                  + new Date());
  }//end ejbRemove()

  public void ejbActivate(){
    System.out.println("HelloBean Activated " 
                                  + new Date());
  }//end ejbActivate()

  public void ejbPassivate(){
    System.out.println("HelloBean Passivated " 
                                  + new Date());

  public void setSessionContext(
                            SessionContext ctx){
    this.ctx = ctx;
      "HelloBean got SessionContext " 
                                  + new Date());

  public HelloBean(){}//public constructor

}//end class HelloBean

Listing 5

/* File HelloHome.java 
Copyright 2000 R.G.Baldwin
Rev 9/15/00
import javax.ejb.*;

public interface HelloHome extends EJBHome{
  public Hello create()
                throws java.rmi.RemoteException, 
}//end interface

Listing 6

/* File Hello.java
Copyright 2000 R.G.Baldwin
Rev 9/15/00
import javax.ejb.*;
import java.rmi.*;

public interface Hello extends EJBObject{
  String aBusinessMethod(String s)
                         throws RemoteException;
}//end interface

Listing 7

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.


Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved