Naming services manage a set of namespaces, resolving name bindings to data objects. The JavaTMNaming and Directory Interface (JNDI) is an API that provides access to naming and directory services. Many Java applications use JNDI to locate resources such as datasources and Enterprise JavaBeans (EJBs) registered in the naming service of a Java 2 Enterprise Edition (J2EE) server.
The IBM®Java thin application client provides a lightweight environment capable of accessing J2EE resources externally via JNDI, enabling you to avoid the overhead of maintaining the J2EE platform on the client machine.
In this section you will create a simple stateless session bean, implement a method, and deploy the EJB to WebSphere®Application Server. The remote thin application client that we will build later in this paper will resolve this EJB via JNDI and invoke the exposed method.
WebSphere Studio Application Developer V5.0 (hereafter called Application Developer) will be used for this exercise. The method of locating and invoking a remote object described in this article is valid only with the Java Runtime Environment included with WebSphere Application Server V5.
Creating a J2EE enterprise application project
Start Application Developer and create a J2EE enterprise application project:
- Select File => New Enterprise Application Project .
-
From the Enterprise Application Project Creation window, select
Create J2EE 1.3 Enterprise Application project
Figure 1. Create a J2EE 1.3 Enterprise Application project

- Click Next .
-
From the Enterprise Application Project Creation window, name the project
Simple. For additional module projects, select EJB Module and name the moduleSimpleEJB
Figure 2. Create an EJB module for the Enterprise Application project.
- Click Finish .
From the Application Developer J2EE Hierarchy view, expand the EJB Modules folder to reveal the newly created module. Associate a new stateless session bean to this module:
- Right-click on EJB Module => SimpleEJBModule and select New => Enterprise Bean .
-
From the Enterprise Bean Creation window, confirm that
SimpleEJB
project is selected:
Figure 4. Create an Enterprise Bean associated to the SimpleEJBModule

- Click Next .
-
From the Create a 2.0 Enterprise Bean window, specify
Session bean
as the bean type. Also specify the bean name as
SimpleEJBand the default package ascom.ibm.simple
Figure 5. Specify the EJB 2.0 type, bean name, and default package.

- Click Next.
- From the Enterprise Bean Details window, do not modify any of the default settings. However, notice the EJB binding name, which will be discussed in more detail below:
Figure 6. Create the remote/home interfaces for the bean.

- Click Finish.
Expand the newly created EJB to reveal the EJB and its remote/home interfaces. Now add a method to the EJB:
-
Double-click on
SimpleEJBBean
to edit the EJB code (see figure 7). After the automatically generated EJB methods, add the following method:
public String echoMe(String whatToEcho) { return("I am echoing: " + whatToEcho); }
Figure 7. Edit the SimpleEJBBean.

-
From the Outline view, right-click on the
echoMe
method, select
Enterprise Bean => Promote to Remote Interface
Figure 8. Promote the method to the Remote Interface.
-
From the J2EE Hierarchy view, right-click on
SimpleEJBModule
and select
Generate => Deploy and RMIC code
Figure 9. Generate the Deployment and RMIC Code.
-
From the J2EE Navigator view, expand the
com.ibm.simple
package and select the following files:
_SimpleEJB_Stub.java, _SimpleEJBHome_Stub.java, SimpleEJB.java, SimpleEJBHome.java
. Right-click and select
Export
Figure 10. Export the generated stubs and EJB Interfaces.
- From the Export window, select JAR file and click Next .
- From the JAR Export window, enter the full path and file name of the JAR:
Figure 12. Define the resources to package into the JAR.
Deploying a J2EE enterprise application
The enterprise application will now be deployed to the WebSphere Application Server Single Server Edition embedded in Application Developer. Follow the instructions below to create and configure the server environment:
- From the J2EE Hierarchy view, right-click on Servers and select New => Server and Server Configuration .
- From the Create a New Server and Server Configuration window, specify the server name as WAS5TestServer and the server type as WebSphere version 5.0 Test Environment:
Figure 14. Create a WebSphere version 5.0 Test Environment.

- Click Finish.
In the J2EE Hierarchy View, expand the Server Configuration folder to reveal the WebSphere Application Server V5 Test Environment Server that you just created. Deploy the enterprise application:
- Right-click on the WAS5TestServer server configuration and select Add => Simple .
- Expand the EJB Modules folder, right-click on SimpleEJBModule , and select Run on Server .
Now we need to prepare the external environment by installing the WebSphere Application Server Application Client V5.0, which is available on the WebSphere Application Server V5.0 install CDs.
After installation, go to the
bin
directory where the Application Client was installed (the default location is
C:\Program Files\WebSphere\AppClient\bin
). In this directory, you'll notice the
setupclient.bat
file, which prepares our thin application client environment. Let#x2019s examine this file more closely:
-
Open the
setupClient.batfile in your favorite editor. Specify the following parameter values (see figure 17):-
WebSphere allows for several different client application types including the pluggable client, J2EE client, and the J2EE Thin client. This exercise illustrates the J2EE Thin client. Ensure that the following parameter value exists:
SET CLIENT_TYPE=J2EETHIN -
The default server name and port number are required. The server name is the accessible hostname or IP address of the server housing the enterprise application. The server port number is the server#x2019s ORB port number. This exercise houses both the server and client on the same machine (your local machine).
SET DEFAULTSERVERNAME=localhost
SET SERVERPORTNUMBER=2809
-
WebSphere allows for several different client application types including the pluggable client, J2EE client, and the J2EE Thin client. This exercise illustrates the J2EE Thin client. Ensure that the following parameter value exists:
- Save the file.
Figure 17. Modify setting parameters in setupClient.bat

-
Open a command window and run the
setupclientbatch file:
Figure 18. From the command line, run thesetupclientbatch file.

We are now ready to externally interact with our EJB. The client application code is shown below and is also available in the archive file associated with this article.
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ibm.simple.*;
import com.ibm.simple.SimpleEJBHome;
public class ExternalCaller
{
public static void main(String[] args)
{
try
{
Context initialContext = new InitialContext();
System.out.println("Calling EJB Externally");
String lookupString =
"cell/nodes/localhost/servers/server1/ejb/com/ibm/simple/SimpleEJBHome";
Object obj =
initialContext.lookup(lookupString);
SimpleEJBHome ejbHome =
(SimpleEJBHome) javax.rmi.PortableRemoteObject.narrow(obj,SimpleEJBHome.class);
SimpleEJB simpleEJB = ejbHome.create();
System.out.println(simpleEJB.echoMe("I worked!"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
} |
The code for locating and invoking our remote object is straightforward. Notice the JNDI name:
cell/nodes/localhost/servers/server1/ejb/com/ibm/simple/SimpleEJBHome
. There are two ways to obtain this string. The first is to run the
dumpnamespace
batch file provided by the WebSphere Application Server Application Client:
-
From the command line, run
dumpnamespace.bat. Locate the JNDI name associated with theSimpleEJBHome
Figure 19. The dumpnamespace displays all objects in the context.
The alternate method is to concatenate the prefix string (
cell/nodes/localhost/servers/server1/
) located in the scope field in the Server Configuration (see figure 20) with the EJB Binding name (
ejb/com/ibm/simple/SimpleEJBHome). The JNDI name can also be found in the Universal Test Client.
One approach would be to seperate the lookupString into two parts, PREFIX and OBJECT,
where PREFIX would be cell/nodes/localhost/servers/server1/
and OBJECT would be ejb/com/ibm/simple/SimpleEJBHome.
Figure 20. The scope can be found in the WebSphere Server Configuration.
Save the file as
ExternalCaller.java
and execute the following command to compile the Java class:
Javac -classpath <location of SimpleEJBStubs.jar>; C:\progra~1\websphere\applclient\lib\j2ee.jar ExternalCaller.java |
Figure 21. Compile the ExternalCaller.java application.
Running the thin application client
To execute our external Java application, make sure your environment is prepared and use a Java command of the following form to invoke the client application:
%JAVA_HOME%/bin/java -Xbootclasspath/p:%WAS_BOOTCLASSPATH% -classpath <list of the referenced jars, classes, and resource directories> -Djava.ext.dirs=%WAS_EXT_DIRS% -Djava.naming.provider.url=iiop://<server:orb port> -Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory %SERVER_ROOT% %CLIENTSAS% <fully qualified main class of the application client>; |
For this exercise, the following command was used. We suggest using a batch file to execute the process -- a batch file called
go.bat
is included in the project ZIP file.
%JAVA_HOME%/bin/java -Xbootclasspath/p:%WAS_BOOTCLASSPATH% -classpath %WAS_CLASSPATH%;c:\jars\SimpleEJBStubs.jar;C:\articles -Djava.ext.dirs=%WAS_EXT_DIRS% -Djava.naming.provider.url=iiop://localhost:2809 -Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory %SERVER_ROOT% %CLIENTSAS% ExternalCaller |
Figure 22. Run the Thin Application Client.
External application clients can access J2EE resources more easily by taking advantage of the IBM Java thin application client. Advantages include environment preparation and minimal footprint on the client machine.
As this article has shown, locating a remote object via a simple Java application in WebSphere Application Server V5 can be a challenge. While our example used an EJB as an example, you can use this approach to locate and retrieve other objects, such as datasources.
Thanks to Troy Bishop, WebSphere Studio Application Developer Support Analyst of the Toronto IBM labs for his help in making this article possible.
| Name | Size | Download method |
|---|---|---|
| ThinClientSample.zip | 59 KB | FTP |
Information about download methods
-
Lookup names support in deployment descriptors and thin clients
-
Developing thin application client code on a client machine
-
The JNDI Tutorial
-
Enterprise JavaBeans Tutorial

Kulvir Singh Bhogal works as an IBM consultant, devising and implementing Java-centric solutions at customer sites across the nation. You can reach Kulvir at kbhogal@us.ibm.com

Kwang Sik Kang is a consultant for IBM Software Services for WebSphere. You can reach Kwang at kkwang@us.ibm.com.




