• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error while running sample EJB client

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to EJB, and trying to to run the sample EJB which returns the test message. I am using Sun application server as an EJB Server, at server side everything is o.k, i am able to deploy the EJB, but when i try to run the client i am getting the following errors
Client Code :
IntialConetext initContext = new InitialContext();
String jndiname = "java:comp/env/ejb/AdviceBean"; // "AdviceBean";
Object objref = initContext.lookup(jndiname);
when i run the client i get the following exception :
D:\projects\headfirst\advice>java -classpath . AdviceClient
exception : javax.naming.NoInitialContextException: Need to specify class name in environment or system property: java.naming.factory.initial
So i tried to add the context propeties like
Hashtable env = new Hashtable();
env.putContext.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, "http://localhost:3641");
IntialConetext initContext = new InitialContext(env);
String jndiname = "java:comp/env/ejb/AdviceBean"; // "AdviceBean";
Object objref = initContext.lookup(jndiname);
When i run like this i am getting this error :
D:\projects\headfirst\advice>java -classpath . AdviceClient
exception : javax.naming.ConfigurationException: http://localhost:3641 does not
contain an IOR
i think the error is due to jndi only,
can anyone suggest me what is the problem.
thanks in advance,
Bharat
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Hmmm, the program doesn't look right to me to begin with. I'll list my observations, then you can decide what to do...
1. Getting the InitialContext.
The way that I use, and what is used all over the place is:
Context c = new InitialContext();
not
InitialContext c = new InitialContext();
2. The JNDI Name
You seem to be using the special JNDI context that is only available to the bean, i.e., java:comp/env/ejb/AdviceBean
Now, since your bean isn't trying to do a lookup on any other bean, I would recommend just using
jndiname = "AdviceBean"
in your client.
3. JARS
Your client doesn't seem to be referencing the stubs that are generated by the deploy process. Do you generate the client side stubs? This is the option that is given to you on deployment (it is grey-ed out normally).
e.g.,
javac -classpath $CLASSPATH:myclientjar.jar myclient.java
and then to run
java -classpath $CLASSPATH:myclientjar.jar myclient
The myclientjar.jar contains the stubs generated by the deploy.
Just a thought on those.
-=david=-
 
bharat kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi david,
thanks for your reply,
i am trying to run the first example given in Head First EJB book, I am using Sun Application Server 1.4 as server, when i run the first time the client i was getting this error
exception : javax.naming.NoInitialContextException: Need to specify class name in environment or system property: java.naming.factory.initial
so i tried with various options.
Here is my client code :
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import javax.ejb.*;
import headfirst.*;
public class AdviceClient {
public static void main(String h[]) {
try {
Context initContext = new InitialContext();
System.out.println("trying to look up EJB");
Object objref = initContext.lookup("AdviceBean");
System.out.println("look up is o.k ");
AdviceHome adviceHome = (AdviceHome)PortableRemoteObject.narrow(objref,AdviceHome.class);
System.out.println("Getting home object is o.k ");
AdviceRemote adviceRemote = adviceHome.create();
System.out.println("Getting remote object is o.k ");
String advice = adviceRemote.getAdvice();
System.out.println("Advice is "+advice);
} catch (Exception h1) {
System.out.println("Exception while running EJB "+h1.getMessage());
}
}
}
The error i am getting is :
D:\projects\headfirst\advice>javac -classpath .;$CLASSPATH:AdviceApplicationClie
nt.jar AdviceClient.java
D:\projects\headfirst\advice>java -classpath .;$CLASSPATH:AdviceApplicationClien
t.jar AdviceClient
Exception while running EJB Cannot instantiate class: org.jnp.interfaces.NamingC
ontextFactory
This is error i am getting, can u suggest what is going wrong, do i have add certain files to my classpath.
After searching this error i added two files jnp-client.jar,jbossall-client.jar to my classpath and run the program again. i am getting timed out error. i am not sure where the JNDI is trying to lookup.
D:\projects\headfirst\advice>javac -classpath %$CLASSPATH%;.;AdviceApplicationCl
ient.jar AdviceClient.java
D:\projects\headfirst\advice>java -classpath %$CLASSPATH%;.;AdviceApplicationCli
ent.jar AdviceClient
trying to look up EJB
Exception while running EJB Receive timed out
But if i deploy the same client program as web component ( as a JSP file ),
i am able to run the client program and get the result.
Thanks in advance,
Bharat
[ January 07, 2004: Message edited by: bharat kumar ]
 
David Harrigan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just to be sure,
%$CLASSPATH%;
is an invalid classpath in Windows, can you remove the $ (which is a UNIX version of putting %% around the env variable).
Can you echo out your classpath
echo %CLASSPATH%
And, you're using JBoss right? Didn't you say you are using Sun Application Server? Does that come with JBoss? The NamingContext error is usually a result of compiling and running in one Container whilst trying to use the Naming factories from another container (i.e., running in WebSphere, but using JBoss NamingContext factories to try and do the lookups...)
-=david=-
 
bharat kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi david,
i have messed with sun application server and jboss, so i removed both of them and removed all the entries in classpath and start a new installtion of J2EE1.3 and Ant tool
After installation i have the following path
ANT_HOME = C:\ant
J2EE_HOME = C:\j2sdkee1.3.1
JAVA_HOME = C:\j2sdk1.4.2
PATH = C:\j2sdkee1.3.1\bin;C:\ant\bin;.;
i did not set any classpath option
when i try to run the server with option j2ee -verbose , i get the following error.
Earlier also when i was running the AdviceClient i was getting the same error, so installed JBoss and added the jnp-client.jar and jboss-clientall.jar to my classpath.
C:\Documents and Settings\bharat>j2ee -verbose
J2EE server listen port: 1050
Naming service started:1050
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interf
aces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: o
rg.jnp.interfaces.NamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
52)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243
)
at javax.naming.InitialContext.init(InitialContext.java:219)
at javax.naming.InitialContext.<init>(InitialContext.java:175)
at com.sun.enterprise.naming.NamingManagerImpl.<init>(NamingManagerImpl.
java:82)
at com.sun.enterprise.server.J2EEServer.run(J2EEServer.java:240)
at com.sun.enterprise.server.J2EEServer.main(J2EEServer.java:972)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFac
tory
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.jav
a:42)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
49)
... 6 more
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interf
aces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: o
rg.jnp.interfaces.NamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
52)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243
)
at javax.naming.InitialContext.init(InitialContext.java:219)
at javax.naming.InitialContext.<init>(InitialContext.java:175)
at com.sun.enterprise.naming.NamingManagerImpl.<init>(NamingManagerImpl.
java:82)
at com.sun.enterprise.server.J2EEServer.run(J2EEServer.java:240)
at com.sun.enterprise.server.J2EEServer.main(J2EEServer.java:972)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFac
tory
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.jav
a:42)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
49)
... 6 more
java.lang.RuntimeException: Cannot instantiate class: org.jnp.interfaces.NamingC
ontextFactory
at com.sun.enterprise.server.J2EEServer.run(J2EEServer.java:346)
at com.sun.enterprise.server.J2EEServer.main(J2EEServer.java:972)
J2EE server reported the following error: Cannot instantiate class: org.jnp.inte
rfaces.NamingContextFactory
Error executing J2EE server ...
C:\Documents and Settings\bharat>

where to get this package org.jnp.interfaces.NamingContextFactory or do i need to add any jar file to my classpath. In Jboss i found this package is available in jnp-client.jar but in J2EE1.3 where to find it.
Thanks for your help,
Bharat.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not need to specify any properties to get the InitialContext when you use Sun's RI Server. The default should be enough. So you don't need to do this:


Hashtable env = new Hashtable();
env.putContext.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, "http://localhost:3641");
IntialConetext initContext = new InitialContext(env);


This should be enough:
Context initContext = new InitialContext();
Maybe that will help you get rid of the classpath trouble.
Hope it helps
/Magnus
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also use Sun RI server and I get a NoInitialContextException if I use the plain version of InitialContext() on the client. I tried different INITIAL_CONTEXT_FACTORY values (rmi, corba, dns & filesystem) but none of them worked fine...
Frank
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using the SUN RI tool? If so, is the following file in your classpath?
appserv-rt.jar
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Robert Ashworth:
Are you using the SUN RI tool? If so, is the following file in your classpath?
appserv-rt.jar


Yes!!!
It worked fine for me on Linux + Sun ONE Application Server 7. Before that I was experiencing the same problems as described above.
Just out of curiosity, Mark, how did you know?...
[ April 10, 2004: Message edited by: Dmitry Danilov ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent !! This did the trick!
Thanks Mark!

Originally posted by Mark Robert Ashworth:
Are you using the SUN RI tool? If so, is the following file in your classpath?
appserv-rt.jar

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Thank you for posting this information. I was having the same problem and it worked for me too after adding appserv-rt.jar to CLASSPATH per your suggestion. I would have wasted a lot of time if I hadn't run into your posting.

Regards,
Hide
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
awsome, adding the appserver-rt.jar file fixed my problem too
 
reply
    Bookmark Topic Watch Topic
  • New Topic