cancel
Showing results for 
Search instead for 
Did you mean: 

problem with initialcontext(NoInitialContextException)

Former Member
0 Kudos

hi,

i have written remote client for ejb.

Context ctx = null;

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialReplicatingContextFactoryImpl");

env.put(Context.PROVIDER_URL, "192.168.1.100:50000");

// env.put("force_remote", true);

env.put(Context.SECURITY_PRINCIPAL, "admin");

env.put(Context.SECURITY_CREDENTIALS, "admin");

try {

ctx = new InitialContext(env);

} catch (NamingException ne) {

System.out.println(ne);

<b>exception is</b>

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

java.lang.NullPointerException

please help me to do this

regards

Guru

<b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b>

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Guru,

You can refer to the following article for getting InitialContext to the J2EE Engine:

http://help.sap.com/saphelp_nw04/helpdata/en/b5/3f48e02b2e8e4d9272ee63819bba80/frameset.htm

I see two problems in your coding:

You use InitialReplicatingContextFactoryImpl and you are trying to connect to the http port of the Engine, while you should connect to the p4 service port. By default the p4 service port on this system should be 50004.

The exception that you get is because you are missing sapj2eeclient.jar in the classpath of the client.

Best Regards

Vyara

Former Member
0 Kudos

hi,

i have changed my programm as you told.But it giving same exception.Please help me to do this.

Properties p = new Properties();

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

hi, i have changed progrmm but it giving same exception.please help me.

p.put(Context.PROVIDER_URL, "localhost:50004");

p.put(Context.SECURITY_PRINCIPAL, "admin");

p.put(Context.SECURITY_CREDENTIALS, "admin");

Former Member
0 Kudos

Hi Guru,

ClassNotFoundException means that the sapj2eeclient.jar file is not in the classpath of the client. Add /usr/sap/<SID>/<InstanceId>/j2ee/j2eeclient/sapj2eeclient.jar:/usr/sap/<SID>/<InstanceId>/j2ee/j2eeclient/exception.jar:/usr/sap/<SID>/<InstanceId>/j2ee/j2eeclient/logging.jar files to the classpath of your client. You might need more jars as well.

Regards

Vyara

Former Member
0 Kudos

I HAVE WEITTEN THE CODE

Properties p = new Properties();

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

p.put(Context.PROVIDER_URL, "192.168.1.100:50004");

p.put(Context.SECURITY_PRINCIPAL, "admin");

p.put(Context.SECURITY_CREDENTIALS, "admin");

try {

ctx = new InitialContext(p );

GetStatusSessionHome objGetStatusSessionHome=null;

Object obj=ctx.lookup("GetStatusSessionBean");

objGetStatusSessionHome=(GetStatusSessionHome)obj;

System.out.println(ctx);

} catch (NamingException ne) {

System.out.println(ne);

EXCEPTION IS

java.lang.NoClassDefFoundError: javax/ejb/EJBHome

at java.lang.ClassLoader.defineClass0(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:539)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)

at java.net.URLClassLoader.access$100(URLClassLoader.java:55)

at java.net.URLClassLoader$1.run(URLClassLoader.java:194)

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

at java.net.URLClassLoader.findClass(URLClassLoader.java:187)

at java.lang.ClassLoader.loadClass(ClassLoader.java:289)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)

at java.lang.ClassLoader.loadClass(ClassLoader.java:235)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

Exception in thread "main"

I HAVE ADDED JARS:SAPJ2EECLIENT.JAR,LOGGING.JAR,EXCEPTION.JAR AND INTERFACES

Former Member
0 Kudos

Hi Guru,

Add ejb20.jar as well.

Regards

Vyara

Former Member
0 Kudos

Hi

Add ejb20.jar also in the classpath , Hence forth please rememember if you are running any stand alone client SAP web AS provides all the reqd jars in

/usr/sap/<SID>/<InstanceId>/j2ee/j2eeclient/

folder which needs to be in the classpath.

Hope this helps , please do not forget to reward points

regards

rajeshkr

Former Member
0 Kudos

IT IS GIVING ANOTHER EXCEPTION PLEASE HELP ME

com.sap.engine.services.jndi.persistent.exceptions.NamingException: Exception while trying to get InitialContext. [Root exception is com.sap.engine.services.security.exceptions.BaseLoginException: Cannot create new RemoteLoginContext instance.]

java.lang.NullPointerException

Former Member
0 Kudos

Hello Guru,

Can you please print the whole stack trace of the exception?

Make sure that you can connect to the Engine with those username and password using the visual admin tool.

Regards, Vyara

Former Member
0 Kudos

please check my code once

Properties p = new Properties();

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

p.put(Context.PROVIDER_URL, "192.168.1.100:50004");

p.put(Context.SECURITY_PRINCIPAL, "admin");

p.put(Context.SECURITY_CREDENTIALS, "admin");

try {

ctx = new InitialContext(p );

GetStatusSessionHome objGetStatusSessionHome=null;

GetStatusSession objGetStatusSession=null;

Object obj=ctx.lookup("java:comp/env/ejb/GetStatusSessionBean");

objGetStatusSessionHome=(GetStatusSessionHome)obj;

objGetStatusSession= objGetStatusSessionHome.create();

objGetStatusSession.getStatus("MAS000002");

} catch(Exception ne) {

System.out.println(ne);

} finally {

try {

ctx.close();

} catch (Exception e) {

System.out.println(e);

}

i have solved all exception by adding jar files.

still it is giving exception

please help me

<b>exception is</b>

com.sap.engine.services.jndi.persistent.exceptions.NameNotFoundException: Path to object does not exist at java:comp, the whole lookup name is java:comp/env/ejb/GetStatusSessionBean.

Former Member
0 Kudos

Hi,

When you use a remote client and when you get a context you can lookup objects from the root element of the tree. and there are no such subcontexts in the root as java:comp/env/.

So you need to lookup the EJB with its application name and EJB name. Refer to the second paragraph of this article:

http://help.sap.com/saphelp_nw04/helpdata/en/38/3e5a4201301453e10000000a155106/frameset.htm

kishorg
Advisor
Advisor
0 Kudos

Hi Guru ,

the exception is because, the given JNDI name is not present in the JNDI registry of the WAS. to avoid this , u have to give the exact JNDI name u have given while deploying ur application .

If u follow like this , u can avoid that type of exception.

U can try this..

In the EJB project double click the ejb-j2ee-engine.xml file . Then in the Enterprise Beans Tab , u can see the EJBs in ur EJB project in 3 sessions like session bean , entity bean and message driven bean.

From this select ur particular bean . then in the right pane , u can give the JNDI name for that ejb.

Give like this..

<u><b>for example...</b></u>

JNDI Name = testJNDI

then u can look up like this in ur code...

<u>a) for Remote Home Object</u>

Object ref =ctx.lookup("testJNDI");

For remote object , u have to type cast this object to Remote Home Object.

<b><u>b) For Local Home object</u></b>

<b>Object ref =ctx.lookup("localejbs/testJNDI");</b>

<b>from ur code given , i could realize that , u r looking for Local Home Object .. so u can use like mentiond above</b>

use like this in ur code...

U can see the JNDI names in the JNDI registry of hur WAS.

This JNDI registry u can see through ,

Visual Administrator or Web Admin ..

ur JNDI name will come under the EAR project ,in which ur EJB is present.

Regards

Kishor Gopinathan

Former Member
0 Kudos

Hi Guru

Log in to the visual admin as an administrator and go the

EJB Container node and check for your deployed ejb , on clicking the ejb you wil be able to get the jndi name of the deployed ejb .

Hope this helps , please do not forget to reward points

regards

rajeshkr

Former Member
0 Kudos

hi,

i want lookup a sessionbean in another sessionbean but both session beans are living in same server.should i add jars(ejb20.jar,logging.jars etc......)to classpath .

regards

Guru

Former Member
0 Kudos

Hi

No you dont have to add any jars , remeember these jars are only for standalone java .

to access other beans u can do a local JNDI look up to the other session bean from the bean class which wants to access that ejb.

For this the other bean class should have the local iterfaces defined as well

Hope this helps .

regards

rajeshkr

Former Member
0 Kudos

hi,

for standalone java program i have to set username,password,and url etc...

{

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

p.put(Context.PROVIDER_URL, "192.168.1.219:50004");

p.put(Context.SECURITY_PRINCIPAL, "admin");

p.put(Context.SECURITY_CREDENTIALS, "admin");

}

is it require for session beans(if both are living in <u><i><b>same server</b></i></u>)

regards

Guru

Former Member
0 Kudos

hi

none of teh setting is required.

Have a look at the code below :

Context ctx = new InitialContext();

2. Look up the bean. To look up the bean locally, use:

MyExampleLocalHome myExampleLocalHome = (MyExampleLocalHome) ctx.lookup(“localejbs/sap.com/MyEAR/MyExampleBean”);

hope this helps .

regards

rajeshkr

Message was edited by: Rajesh K. R.

Former Member
0 Kudos

hi,

in look up what shoud i use whether <i><b>jndi</b></i> name(which is given to ejb)or <i><b>earname/sessionbeanname</b></i>

please tell me clearly

regards

Guru..

Former Member
0 Kudos

it should be <b>localejbs/jndiname</b>

the jndi name is one that you specify in ejb-j2ee-engine.xml

Note : You do not specify <b>localejbs</b> in ejb-j2ee-engine.xml

Former Member
0 Kudos

Hi

You can check your deployed EJb's jndi name through visal admin . FOr this go to the JNDI registery node & on the right hand screen scroll down and click on the <i>localejbs</i> node(blue clolor) . check for ur deployed ejb context in that node .

for ex if u see <i>ejb</i> as the next node which has one more child node <i>MyLocalEjb</i>

then your jndi name will be <b><i>localejbs/ejb/MyLocalEjb</i></b>

Hope this helps

regards

rajeshkr

Former Member
0 Kudos

hi,

i have written code as said.(glocalejbs/MyLocalEjb(i have seen in <b>visual administrator</b>))

but the error is <i><b>class cast exception</b></i>.<u></u>

regards

Guru

Former Member
0 Kudos

Hi

I thnk u have not put the bean name . If you see in visual admin u will the last node in red which will give u [classname]:com.XXXX.MYEJBLocalHomeImpl .

Please check that the JNDI name will include uptill the last red node .

hope this helps

regards

rajeshkr

Former Member
0 Kudos

did you typecase to the home interface after lookup?

you need to use code like this

Object ref = ctx.lookup("<b>JNDI_NAME</b>");
// Cast to Home interface
home = (<b>HOME_INTERFACE</b>) PortableRemoteObject.narrow(ref, <b>HOME_INTERFACE</b>.class);
remote = home.create();

Former Member
0 Kudos

hi,

i have given the jndi name as in visual admin.

But it is giving

<b>NameNotFoundException</b>

i have look up 4 sessionbeas with jndi names.3 are working fine.1 is giving NameNotFound exception.

Please help me to do this

regards

Guru

Former Member
0 Kudos

Hi

Please check whethre the JNDI name which you are referring and the one which you see in visal admin is the same for local & remote ejbs . I thnk your JNDI name by which you are accessing in code is incorrect

Hope this helps , please mark points for helpful ans.

regards

rajeshkr

Former Member
0 Kudos

hi,

i have solved all problems with u r help.but it is giving error like

<i><b>java.lang.reflect.UndeclaredThrowableException</b></i>

regards

Guru

Former Member
0 Kudos

Hi

Did you catch NamingException in a try catch block

try

{

MyExampleLocalHome myExampleLocalHome = (MyExampleLocalHome) ctx.lookup(“localejbs/sap.com/MyEAR/MyExampleBean”);

}catch (NamingException ne)

{

e.printStarckTrace();

}

hope this helps , please mark helpful answers

regards

rajeshkr

Answers (2)

Answers (2)

Former Member
0 Kudos

hi,

have you included the client jar .

you need to obtain the client JAR in order to run the beans from within the client application.

the procedure for obtaining client jar is:

http://help.sap.com/saphelp_nw04/helpdata/en/5f/2dd984b5d1304b9155f161568f2f64/frameset.htm

Former Member
0 Kudos

hi guru

1)If you are accessing the ejb from a standalone client (java program) check whether sapj2eeclient.jar is your classpath ,since the context factory impl class is in this jar.

2) In your code the please check the p4 port is 50000 . you can check this from visual admin .The default port is 50004.

hope this helps , please do not forget to reward points if helpful.

regards

rajeshkr