 | Level: Advanced Paul Pacholski (pacholsk@ca.ibm.com), Technical Sales Consultant, IBM Elson Yuen (eyuen@ca.ibm.com), software developer, IBM Toronto Lab, IBM
01 Mar 2004 This article describes a simple and elegant technique for reusing code in external EAR applications within WebSphere Studio Application Developer. Rather than importing large EAR applications into the Application Developer workspace, it is more efficient to keep them outside and reference them. This article shows you how, and covers development and testing considerations, including compiling code that references external EAR applications and running them in the WebSphere Test Environment.
Introduction
If you are a C++ programmer, do you remember how C++ frameworks and class libraries were packaged and distributed? They were compiled as DLLs
(dynamic link libraries). To distribute a class library, you would simply provide the DLL and the C++ include files. To call a method from a framework in a DLL, all you needed to do is to compile your C++ code with the framework's include files. Simple and easy!
How does a J2EE application developer, using IBM ® WebSphere® Studio Application Developer, build and test J2EE applications that reuse code
in an external EAR applications?
This article describes an simple and elegant technique for reusing code in external EAR applications with WebSphere Studio
Application Developer (hereafter called Application Developer). Rather that importing several large EAR applications into the Application Developer workspace it is
more efficient, from the point of view of resource use, to keep them outside and reference them. This article shows you how to do that, and covers development and testing considerations, including compiling code that references external EAR applications and running them in the WebSphere Test Environment. Although external EAR applications and the technique described in this article and are not formally supported by WebSphere Studio, they will be useful to users who are interested in exploring the flexibility of the product.
The external EAR application technique is described in five easy steps:
- Create and configure WebSphere Application Server V5.0.
- Add external EAR file to WebSphere Application Server.
- Expand the external EAR file.
- Compile the
earProj2 EAR application.
- Test the Web and EJB modules in the external EAR application.
The procedures in this article require the use of WebSphere Studio Application Developer for Windows, V5.0.1 or V5.1, but no familiarity with the basic operations of Application Developer is required.
The external EAR application test scenario
The test scenario in this article uses two EAR applications: earProj1 and earProj2. earProj2 is loaded into Application Developer, and earProj1 resides outside of Application Developer, but is loaded by the WebSphere Test Environment to test the earProj2 application in Application Developer. earProj2 contains one session EJB called TestBeanTester. earProj1 EAR contains a single session EJB called TestBean:

The testTestBean() method makes a call (via a remote interface) to the hello() method in the external EAR application. To demonstrate
that a method call can be made to an external EAR, we will launch the TestBeanTester EJB in the WebSphere Universal Test Client.
The earProj1 EAR application also contains a Web application with a single index.jsp file. To show that we can launch a Web
application from an external EAR, we will launch the index.jsp file in the WebSphere Test Environment.
Step 1. Create and configure WebSphere Application Server V5.0
- Create WebSphere Application Server V5.0 server and server configuration:
- Start Application Developer and switch to the Server perspective.
- From the main toolbar, click on the Create server and server configuration button
.
- On the "Create a new server and server configuration page," enter
WAS5 for server name and
WebSphere version 5.0 => Test Environment for Server type:

- Click Finish.
- Configure WebSphere Application Server V5.0:
- Switch to the Servers view and double-click on the WAS5 server. The Server Configuration Editor opens:

- In the Server Configuration Editor, click the Configuration tab.
- In the Server Configuration section, check the Enable administration console checkbox, because we
will be launching the WebSphere Administration Console.
- In the Server Settings section, select SINGLE for Application class loader policy, because we will be calling an EJB from another EAR application:

- Press Ctrl-S to save and close the Server Configuration Editor.
- Import the earProj2 EAR application and add it to the WAS5 server:
- From the main menu bar, select File => Import.
- On the Select page of the Import Wizard, select EAR file and click Next.
- On the Enterprise Application Import page, click Browse to navigate to the second EAR application
(
earProj2.ear) supplied with this article:

- Click Finish.
- In the Server Configuration view, right-click on WAS5 server and select Add and remove projects.
- On the Add and Remove Projects page of the Add and Remove Projects Wizard, select earProj2 and click Add>.
This will add
earProj2 to the Configure projects list:

- Click Finish. You will see compile errors in the Task view -- we will fix them later.
 |
Step 2. Add external EAR to WebSphere Application Server
- Launch the WebSphere Administrative Console:
- From the Servers view, select WAS5 server and from the Servers toolbar, click the Start the server button
. This action will start the WebSphere Unit Test Environment.
- After you see message
Server server1 open for e-business in the Console view, switch to the Servers view.
Right-click on WAS5 server and select Run administrative console.
- Launch the WebSphere Administrative Console.
- In the Login window, for User ID, enter your user name and click OK.
- In the WebSphere Application Server Version 5 Administrative Console, from the left pane, select Applications => Install New Application:

- On the Preparing for the application installation page, select the Local path button.
- Click Browse to navigate to the first EAR application (
earProj1.ear) supplied with this article:

In the example above, we moved earProj1.ear to the F:\tem\EARs directory.
- Click Next .
- On the second Preparing for the application installation page, click Next.
- On the Step 1 page enter:
| Item | Value | | Directory to Install application | Enter an existing directory, for example F:\temp\MyInstalledApps. | | Use Binary Configuration | enable | | Create MBeans for Resource | disable |
- Use Binary Configuration
- The application server will use the binding, extensions, and deployment descriptors in
the application deployment document, the
deployment.xml file (default), or the EAR file.
- Create MBeans for Resources
- We will not create MBeans for various resources (such as servlets or JSPs) within an application when the application is started. JMX MBeans represent the
management interface for a particular piece of logic. All of the managed resources within the standard WebSphere Application Server infrastructure
are represented as JMX MBeans.

- Click Next.
- On the Step 2 page, click Next.
- On the Step 3 page, click Next.
- On the Step 4 page, check the earProj1EJB and earProj1Web checkboxes:

- Click Next.
- On the Step 5 page, click Next.
- On the Step 6 page, click Finish.
- After you see a message similar to this:

Click Save on the Administrative Console main toolbar:

- On the Save to Master Configuration pane, click Save:

- From the Servers view, select WAS5 and from the Servers toolbar click Stop the server
.
If the external EAR application requires configuring data sources,
JMS queues, and so on, use the Server Configuration Editor or the WebSphere Administrative Console.
 |
Step 3. Expand the external EAR
In the previous step, we set the external EAR's install application directory to F:\temp\MyInstalledApps.
In this section, we will be using the WebSphere EARExpader.bat file to expand earProj1.ear
to that directory.
- Modify the EARExpander invoker file
expand.bat:
- Using an editor of your choice, open the
expand.bat file:

- Change
E:\WSAD51 to your WebSphere Studio Application Developer V5.1 install directory.
- Change
F:\temp\MyInstalledApps to the directory you specified above in step 2.2.7.
- Save the changes.
- Run the EARExpander invoker file
expand.bat.
- Run
expand.bat.
- Examine the expanded EAR application. When fully expanded, the directory you specified as the "Directory to Install
Application" in step 2.2.7 should look similar to this:


Step 4. Compile the earProj2 EAR application
- Examine the compile errors:
- In the lower right pane of the Server perspective, click the Task tab:

- Examine the compile errors:

These errors occur because the packages that define the TestBean remote interface and TestBeanHome classes are not included in
the Application Developer workspace; they reside in the external EAR (earProj1).
- Click on the first error in the Task view to open the
TestBeanTesterBean.java file and note the missing packages:

- Add
earProj1EJB.jar to the build classpath:
- In the Navigator view, right-click on earProj2EJB and select Properties:

- In the Properties for earProj2EJB window, from the left pane, select Java™ build path.
- In the right pane, click the Libraries tab
and
then click Add External JARs.
- In the JAR Selection window, navigate to the directory where you expanded the
ear1Proj.ear file in
Step 3.2above (in this example, to the F:\temp\MyInstalledApps\ear1Proj.ear\ directory).
Then select earProj1EJB.jar:

- Click Open.
- In the Properties for earProj2EJB window, click OK to add
earProj1EJB.jar to the build classpath of the
earProj2 EJB module. Application Developer rebuilt the EJB module. All compile errors have now been fixed,
and the Tasks view shows no errors.
 |
Step 5. Test the Web and the EJB modules in the external EAR application
- Update the server configuration:
- In Step 2 above, you installed the
ear1Proj EAR application in the WebSphere Administrative Console.
We will now update the Application Developer server configuration to reflect that change. Switch to the Server Configuration view and double-click
on the WAS5 server:

- Click Yes on the WebSphere Server Warning message box:
.
- In the Server Configuration Editor, press ctrl-S to save changes and then close the Server
Configuration Editor. In the Server Configuration view, the external EAR application
earProj1 now appears:

- Run Web application in the external EAR:
- In the lower right pane of the Server perspective, click the Servers tab.
- Select the WAS5 server, and from the Servers toolbar, click the Run the server button
.
In the Console view, you should now see message indicating that the earProj1 was loaded successfully:

- From the main toolbar, click the Open Web Browser button
.
- In the Web Browser window, enter
http://localhost:9080/earProj1Web/index.jsp and press Enter. You should now see
index.jsp from the Web application in the ear1Proj EAR application displayed in the Web Browser window:

- Invoke session EJB in the external EAR:
- Click on the J2EE Perspective button
in the top right corner
to switch to the J2EE perspective.
- In the J2EE hierarchy view, select EJB Modules => earProj2EJB => Session Beans => TestBeanTester:

- Right-click and select Run on Server to start the WebSphere Universal Test Client.
- In the Server Selection window, click Finish.
- In the IBM Universal Test client Web application window, click TestBeanTesterLocal:

then click TestBeanTesterLocalHome:

and finally click TestBeanTesterLocal create():

- In the Parameters pane, click Invoke:

and then click Work with Object:

- On the References pane, click String testTestBean(String):

- On the Parameters pane, enter
Value from external EJB and click Invoke:

- Switch to the Console view. You should see a message from the Session EJB (TestBean) printed in the Console window:

 |
Conclusion
This article showed you how to use an EAR application that is external to Application Developer. It addressed the compile considerations
that arise when an EJB developed in an Application Developer workspace calls an EJB in an external EAR application. It also addressed
testing considerations, and showed you how to configure the WebSphere Unit Test Environment server to load an external EAR application. Finally, it showed that, using the techniques described in this paper, you can invoke a Web application and an EJB from an external EAR application.
We have not shown how to access the contents of the WAR applications in an external EAR from the Application Developer workspace for WAR modules. We cannot simply point to the WAR file, since the classes are stored under an enclosed directory structure. We must expand the WAR file and point to the
WEB-INF/classes directory in the expanded structure. Because both steps have already been discussed in Expand the External EAR above, you can use the contents of that directory to resolve the build classpath of the internal Application Developer projects.
Download | Name | Size | Download method |
|---|
| earProj.zip | 0.1 MB | FTP | HTTP |
About the authors  | 
|  | Paul Pacholski is a technical sales consultant supporting WebSphere development tools and specializing in Web services and
WebSphere Business Integration products. You can reach Paul at pacholsk@ca.ibm.com. |
 | 
|  | Elson Yuen is a software developer in the IBM Toronto Lab working on WebSphere Studio Application Developer. He is
currently designing and developing the server tooling, including the WebSphere Test Environment. Previously, he was the key
developer on the VisualAge® for Java™ WebSphere Test Environment JSP debugging support and the WebSphere Test Environment Control
Centre. Elson has a Bachelors degree in Electrical Engineering with Management Science options from the University of Waterloo, Ontario.
You can contact Elson at eyuen@ca.ibm.com.. |
Rate this page
|  |