Published January 22, 2001
By Richard G. Baldwin
Java Programming, Lecture Notes # 1315
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 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.
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.
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:
deploytool
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.
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:
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
<application>
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"?>
<j2ee-ri-specific-information>
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:
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,
<ejb-jar>
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.
/*
File HelloBean.java
Copyright 2000 R.G.Baldwin Rev 9/15/00 This bean receives and
returns a String object
This is a stateless session
bean. According to
Implement the SessionBean
interface.
Tested using Sun j2ee
and JDK 1.3 under
public class HelloBean
implements SessionBean{
public void ejbCreate(){
public String
aBusinessMethod(String str){
public void ejbRemove(){
public void ejbActivate(){
public void ejbPassivate(){
public void setSessionContext(
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{
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{
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.
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.
-end-