cancel
Showing results for 
Search instead for 
Did you mean: 

Calling EJB from from a client app

Former Member
0 Kudos

Hi all,

I am trying to call an EJB component from portal. The example EJB I have taken is from SDN Bonus Calculator example - Application Server/Web Dynpro/Samples and Tutorials/Using EJBs (20)

I deployed the EAR file - no issue

I wrote the following code in a client app and trying to connect to the EJB remotely.

try {

java.util.Properties properties = new Properties();

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

properties.put(Context.PROVIDER_URL, "ux0800:55304");

InitialContext ctx = new InitialContext(properties);

//InitialContext ctx = new InitialContext();

// get ejb home

home =

(BonusCalculatorLocalHome) ctx.lookup(

"localejbs/MySessionBean");

theCalculator = home.create();

} catch (Exception namingException) {

namingException.printStackTrace();

}

I get the following error

javax.naming.NoInitialContextException: Cannot instantiate class: com.sap.engine.services.jndi.InitialContextFactoryImpl [Root exception is java.lang.ClassNotFoundException: com.sap.engine.services.jndi.InitialContextFactoryImpl]

at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

at javax.naming.InitialContext.init(Unknown Source)

at javax.naming.InitialContext.<init>(Unknown Source)

at com.sap.bonus.calculation.sessionBean.MyCommandBean.<init>(MyCommandBean.java:27)

at com.sap.bonus.calculation.sessionBean.MyCommandBean.main(MyCommandBean.java:68)

Caused by: java.lang.ClassNotFoundException: com.sap.engine.services.jndi.InitialContextFactoryImpl

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)

Any clue will be appreciated.

thnx

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi,

I faced the issue long ago, Please add all the jars in the j2eeclient folder in your machine.

possibly here.

C:\usr\sap\F48\JC30\j2ee\j2eeclient.

This should solve the problem.

regards

Vivek Nidhi.

Former Member
0 Kudos

hi,

<a href="http://help.sap.com/saphelp_nw04/helpdata/en/64/620a2ae361344abf47462809eb5388/frameset.htm">Generating J2EE Components</a>

<a href="http://help.sap.com/saphelp_nw04/helpdata/en/b7/9877fe13221244838a16e3683ad344/frameset.htm">J2EE Components Deployment Properties Management</a>

This may help u

Regards,

Beevin.

Former Member
0 Kudos

Hi Sabbir, if you are still interested:

You probably miss some additional jars containing the desired class:

From the J2EE client JARs:

ejb20.jar, exception.jar, guidgenerator.jar, jARM.jar, jperflib.jar,

jta.jar, log_api.jar, logging.jar, sapj2eeclient.jar, sapni.jar,

sapxmltoolkit.jar

They can be found under e.g. C:\usr\sap\J2E\JC00\j2ee\j2eeclient\signed. Add them as external libs

Former Member
0 Kudos

Hi Sabbir,

you can easy find if the object(ejb) is bound in the j2ee's naming context.

make a telnet connection to the j2ee engine

jump to the server node

execute that:

add naming

and call

lookup <name-that-you-try-to-lookup>

the result will show you whether you have your object in the naming or not.

best regards,

mladen

Former Member
0 Kudos

Hi Mladen,

Could you please explain a bit more in detail this procedure (to check ejb's existance in jndi)?

Thanks

Former Member
0 Kudos

Hi Sabbir,

Instead creating objects of LocalHome and Local, create objects of Home and Remote like this

home = (BonusCalculatorHome) ctx.lookup("localejbs/MySessionBean");

theCalculator = (BonusCalculatorRemote)home.create();

Where home is of type BonusCalculatorHome and theCalculator is BonusCalculatorRemote

Regards,

Uma

Message was edited by: Uma Maheswari

suresh_krishnamoorthy
Active Contributor
0 Kudos

Hi Sabbir,

Exception:Object not found in lookup of TestJNDI. did you gave JNDI name and then you deployed your EJB application. because TestJNDI is registered in WAS.

Regards, Suresh

Former Member
0 Kudos

1. Only <i>InitialContext ctx = new InitialContext();</i> should work. no need of putting properties.

2. If you have local interfaces for your ejb , then you should look it up as

home =
(BonusCalculatorLocalHome) ctx.lookup(
"localejbs/MySessionBean");

as you specified, else

home =
(BonusCalculatorLocalHome) ctx.lookup(
"java:comp/env/MySessionBean");

should work with remote interfaces.

suresh_krishnamoorthy
Active Contributor
0 Kudos

Hi Sabbir,

Following code works for me in calling EJB from normal java application. Did you added the jar files to your application?. see the required jar files in end of code.

Create simple java applcation do the following steps:

Give JNDI name to the EJB(HelloJNDI)->Add the HelloEJB to HelloEAR ->deploy

Proxy Program to call ejb:

import javax.naming.Context;

import javax.naming.InitialContext;

import com.sample.Hello;

import com.sample.HelloHome;

public class HelloTest {

public static void main(String[] args) {

Hello remote = null;

try {

Context ctx = new InitialContext();

HelloHome home = (HelloHome) ctx.lookup("HelloJNDI");

remote = home.create();

String result = remote.hello();

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

} catch (Exception e) {

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

}

}

}

1. Go To Run > Java Application ->New->select your project and select your main class(java program from which your going to call EJB)

2. Click next tab ->(x)=Arguments

paste the following code in VM Arguments

-Djava.naming.factory.initial=com.sap.engine.services.jndi.InitialContextFactoryImpl -Djava.naming.provider.url=localhost:50104

replace the server IP address with your server IP address where your ejbs are running.

Under Class path settings for the program put the following jar files.

You can search for them from net weaver soruce folders

or copy from D:\usr\sap\J2E\JC01\j2ee\j2eeclient

ejb20.jar

logging.jar

exception.jar

sapj2eeclient.jar

Add Corresponding ejb.jar

and RUN >>>

this makes testing of your client programs easier, you can find the error trace on which line and saves lot of time.

Regards, Suresh

Former Member
0 Kudos

Suresh,

I tried your suggestion.

Now I am getting the following error

Exception:Object not found in lookup of TestJNDI.