cancel
Showing results for 
Search instead for 
Did you mean: 

Calling an EJB...

david_fryda2
Participant
0 Kudos

Hi everyone,

I succeed calling a simple stateless EJB from a servlet or JSP.

I have a problem calling it from a Java class that is not a servlet or JSP.

This Java class sites on a computer that is not the J2EE engine server.

My enterprise application project is called "MyCalculatorEAR" and the vendor name is "sap.com".

My EJB modul project is called "MyCalculatorEJB".

The bean is called "CalculatorEJBBean".

Here is the code :

try {

Properties props = new Properties();

props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sap.engine.services.jndi.InitialContextFactoryImpl" );

props.put(Context.PROVIDER_URL,"server:50004");

props.put(Context.SECURITY_PRINCIPAL, user);

props.put(Context.SECURITY_CREDENTIALS, password);

InitialContext ic = new InitialContext(props);

Object ob = ic.lookup("sap.com/MyCalculatorEAR/CalculatorEJBBean");

ic.close();

} catch(Exception e) {

e.printStackTrace();

}

System.out.println("END");

It take about 8 long minutes until it success to make the lookup.

If i make a lookup on Object ob = ic.lookup("sap.com/MyCalculatorEAR") it does it very fast.

After waiting for the first lookup, I wrote :

CalculatorEJBHome home = (CalculatorEJBHome) PortableRemoteObject.narrow(ob,

CalculatorEJBHome.class);

And I get a ClassCastException.

Can someone help me.

NB : it is not my first post about this subject. I've been told that maybe my jar files aren't the good one (the ejb20.jar or the p4.jar).

Thanks.

Accepted Solutions (0)

Answers (3)

Answers (3)

SvetoManolov
Employee
Employee
0 Kudos

Hi,

please have a look at the following article - it explains how to create EJB clients.

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/java/accessi... ejb applications using jndi.pdf

Indeed, accessing an EJB from a non-J2EE environment is not defined by the J2EE spec. The scenario is supported by the SAP J2EE Engine, but I would recommend you to use it only for testing purposes and not to rely on it for productive usage.

I hope this will help.

Best regards,

Svetoslav

Former Member
0 Kudos

David, make sure your classpath is set correctly. Here's a snip from a batch file I use to ensure that the classpath is correct:

set ROOT=d:/usr/sap/j2e/jc00/j2ee/j2eeclient

set CLASSPATH=.;%ROOT%/sapj2eeclient.jar

set P=d:\usr\sap\j2e\jc00\j2ee\j2eeclient

set CLASSPATH=%CLASSPATH%;%P%\logging.jar

set CLASSPATH=%CLASSPATH%;%P%\ejb20.jar

set CLASSPATH=%CLASSPATH%;%P%\exception.jar

rem

rem The following jars are from my application

rem

set CLASSPATH=%CLASSPATH%;D:\usr\ws\com.test.ejb\com.test.ejb.jar

set CLASSPATH=%CLASSPATH%;D:\usr\ws\com.test\com.test.jar

set CLASSPATH=%CLASSPATH%;D:\usr\ws\com.test.shared\com.test.shared.jar

set CLASSPATH=%CLASSPATH%;D:\usr\ws\AnotherEJBProject\AnotherEJBProject.jar

I've included the J2EE client libraries, *as well as* my EJB library, which contains the declarations for the home interface. Unfortunately, I don't think you can split the EJB jar files so that one includes the implementation, and one includes the interface. If this was the case, you could just reference the interface, and all would be well.

This has issues if you plan on accessing the J2E engine from another JRE - you must unfortunately distribute the entire jar file, which means if anything changes in the implementation, you must redistribute the entire jar file.

Hope that helps you out.

nitzanlevi
Explorer
0 Kudos

Hi,

it is very hard to find the problem from what you describe, can you add the ClassCastException stacktrace? have you checked that your JNDI entry is not for the local interface (so you need to cast it to CalculatorEJBLocalHome ), if so even id the casting will be succesfull you won't be able to work with this bean because you need the remote one. so i would check the folowings:

1. that you have all the right jar files in your client application (the engine jars and the EJB client jars)

2. that the JNDI name you are using is for the Remote Interface and not for the Local.

hope it helps you.

Former Member
0 Kudos

You are trying to call a J2EE resource from a non J2EE environment. I am not sure this is possible. I believe the minimum required level is to be a J2EE client application.

Check out your J2EE provider's way to build and call J2EE client apps. Some providers have a heavier footprint than others...

Enjoy