Você está na página 1de 3

Testing EJBs using JNDI lookup

Introduction

In Webdynpro - Adaptive web service, we have created EJB Module project for business logic and
exposed the EJB as web service. Debugging web service via web service navigator is found to be
cumbersome due to inadequacy of error trace and it also consumes time. To make it optimal we
have created a Standalone Java program to test EJB the business methods using JNDI lookup.

Prerequisites:

- An EJB Module Project and specify the JNDI name in deployment descriptor file ejb-j2ee-
engine.xml.

- Create an Enterprise Application Project (EAR), with reference of EJB Module Project and deploy to
the SAP J2EE Engine.

Creating standalone java program

Use SAP NetWeaver Developer Studio:

1. From the menu path, choose File -> New -> Project.

2. In the dialog that appears select Java -> Java Project and choose Next

3. Specify SampleTest as the Project name. Choose Finish. The new project is created.

4. Select the name of the project and open the context menu. Choose New -> Class.

5. Specify the Java package for Standalone java project class and name it com.sap.test.

6. In the dialog that appears, specify Sample as the name of the class. Choose Finish.

7. Now the Java class for JNDI lookup has been generated in the specified Java package.

8. Right click on SampleTest project -> Properties -> Java Build Path -> Select the Project tab ->
Select the EJB Module Project and click Ok.

9. Also add the following External Jar files and path is <Server
Drive>:/usr/sap/J2E/JC00/j2ee/j2eeclient

- ejb20.jar
- logging.jar
- exception.jar
- sapj2eeclient.jar

10. Make sure that JRE system libraries is set.


Standalone Java class source code:

package com.sap.test;

import javax.naming.Context;

import javax.naming.InitialContext;

import com.test.SampleEJB;

import com.test.SampleEJBHome;

public class Sample {

public static void main(String[] args) {

SampleEJB remote = null;

try

// Create a new intial context, which loads from jndi.properties file.

Context ctx = new InitialContext();

// Look up the home interface using the JNDI name.

// This JNDI lookup returns a reference to an EJBHome instance.

SampleEJBHome home = (SampleEJBHome) ctx.lookup("SampleJNDI");

// Create a session object.

remote = home.create();

// Invoke the remote EJB methods, test and debug.

String result = remote.Add(2,4);

System.out.print("Result:" + result);

catch (Exception e) {

System.out.println("Exception: " + e.getLocalizedMessage());

}
}

Run the Java Program:

1. From the menu path, choose Run -> Run -> Create, manage and run configurations.

2. Select Java Application -> Click on New button in bottom

3. Under Main tab: Enter the Name as Sample, Browse and select the Java project SampleTest
and Browse and select the main class com.sap.test.Sample under the Java Project.

4. Under ( X ) = Arguments tab: Specify the following environment properties details in VM


Arguments.

-Djava.naming.factory.initial=com.sap.engine.services.jndi.InitialContextFactoryImpl

-Djava.naming.provider.url= <Server IP Address> :< P4 Port no>

(For example: -Djava.naming.provider.url= 10.6.1.14:50004).

5. Click on Apply and Run. Result is shown in the NWDS console perspective.

Exceptions and solutions:

Exception: Exception: Exception while trying to get InitialContext.

Solution: Check whether the server is up and running, Check the VM Arguments like Server IP
addresses and Portno is correct.

Exception: Exception: Cannot instantiate class:


com.sap.engine.services.jndi.InitialContextFactoryImpl

Solution: Add sapj2eeclient.jar file to the Java project

Exception: Fatal exception occurred. Program will exit: java.lang.NoClassDefFoundError:


com/sap/exception/IBaseException

Solution: Add exception.jar file to the Java project

Exception: Fatal exception occurred. Program will exit: java.lang.NoClassDefFoundError:


com/sap/tc/logging/LogController

Solution: Add logging.jar file to the Java project

Reference: SAP Help Link: Creating Your First J2EE Application

http://help.sap.com/saphelp_nw04/helpdata/en/7d/cf0c8abcc34594ba9d3bbd5dd22155/frameset.ht
m

Você também pode gostar