This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes NoInitialContextException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "NoInitialContextException" Watch "NoInitialContextException" New topic
Author

NoInitialContextException

Ahamed Sharif
Ranch Hand

Joined: Aug 24, 2003
Posts: 47
Hello,
I deploy a bean successfully, but when I run the client program it was
giving the runtime exception like
javax.naming.NoInitialContextException: Need to specify class name in
environmen or system property,or as an applet parameter, or in an
application resource file: java.naming.factory.initial
Client source code:
package test;
import javax.naming.InitialContext;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class Main
{
public static void main(String[] args)
{
try
{
// Get a naming context
InitialContext jndiContext = new InitialContext();
System.out.println("Got context");
// Get a reference to the Interest Bean
Object ref = jndiContext.lookup("First");
System.out.println("Got reference");
// Get a reference from this to the Bean's Home interface
TestHome home = (TestHome)
PortableRemoteObject.narrow(ref, TestHome.class);
// Create an Interest object from the Home interface
TestRemote call = home.create();
System.out.println("Process strat");
String msg = call.process();
System.out.println(msg);
System.out.println("Process end");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
I am not understanding that exception any one can help me.
Thanks,

Sharief
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

I guess it is because you havent passed Properties (The jndi implementation class etc)to InitialContext contructor.


Groovy
Ahamed Sharif
Ranch Hand

Joined: Aug 24, 2003
Posts: 47
Hello,
But I dont know how to pass jndi properties class because i am new to ejb,
so please could u show me how to write and pass the jndi properties class.
Thanks,
Sharief
Nauman Khan
Ranch Hand

Joined: Aug 07, 2003
Posts: 49
Hi,
this is a simple code,
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
//props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming rg.jnp.interfaces");
props.put(Context.PROVIDER_URL, "localhost:1099");
InitialContext context=new InitialContext(props);
Set the properties value according to your application server,I'm using the jboss that's why the above values are set in my properties object,so make sure these values should match to your application server jndi properties.
Regards,
Nauman


Nauman Khan <br />SCJP,MCSE,SCWCD,SCBCD,SCWCD1.4
Alka S.
Greenhorn

Joined: Nov 29, 2003
Posts: 26
Hi,
I am using same code for InitialContext as Nauman said as follows-
Weblogic as Application server and tomcat as web server.
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
InitialContext initCtx = new InitialContext(p);
Object obj = initCtx.lookup("myFacilityService");
FacilityServiceHome home = (FacilityServiceHome)PortableRemoteObject.narrow(obj,
FacilityServiceHome.class);
//Create the remote interface instance
FacilityService cli = home.create();
cli.getAllFacility();
httpServletRequest.setAttribute("FacilityList", cli.getAllFacility());

I have copied jar in Web-WEB-INF- lib and weblogic.jar in Tomacat-common-lib
getting error as follows
java.lang.NullPointerException
at weblogic.rmi.internal.StubGenerator.getStubOnClient(StubGenerator.java:694)
at weblogic.rmi.internal.StubGenerator.getStubClass(StubGenerator.java:666)
at weblogic.rmi.internal.StubGenerator.generateStub(StubGenerator.java:712)
at weblogic.rmi.internal.StubGenerator.generateStub(StubGenerator.java:699)
at weblogic.rmi.extensions.StubFactory.getStub(StubFactory.java:76)
at weblogic.jndi.WLInitialContextFactoryDelegate.newRootNamingNodeStub(WLInitialContextFactoryDelegate.java:486)
at weblogic.jndi.WLInitialContextFactoryDelegate.newRemoteContext(WLInitialContextFactoryDelegate.java:449)
at weblogic.jndi.WLInitialContextFactoryDelegate.newContext(WLInitialContextFactoryDelegate.java:345)
at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:308)
at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:234)
at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:135)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:250)
at javax.naming.InitialContext.init(InitialContext.java:226)
at javax.naming.InitialContext.(InitialContext.java:202)
at com.powermanage.mcaccess.portal.AnnotationAction.execute(AnnotationAction.java:73)
Ahamed Sharif
Ranch Hand

Joined: Aug 24, 2003
Posts: 47
Hi,
I set the properties but still getting some errors,
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory. Root exception is java.lang.ClassNotFoundException:
org.jnp.interfaces.NamingContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:217)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.jav
a:42)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
49)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243
)
at javax.naming.InitialContext.init(InitialContext.java:219)
at javax.naming.InitialContext.<init>(InitialContext.java:195)
at test.Main.main(Main.java:37)
And I set the classpath for jboss-client.jar like
>set classpath=.;C:\jboss-3.0.8_tomcat-4.1.24\client\jboss-client.j
ar;%classpath%
and the jndi.properties in C:\jboss-3.0.8_tomcat-4.1.24\server\cinfig\conf
so i set the classpath for that also.
jndi.propeerties files looklike this,
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming rg.jnp.interfaces
# Do NOT uncomment this line as it causes in VM calls to go over
# RMI!
#java.naming.provider.url=localhost

and The client code is,

package test;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import javax.naming.*;
import test.TestRemote;
import test.TestHome;
public class Main
{
/** This method does all the work. It creates an instance of the Interest EJB on
the EJB server, and calls its `calculateCompoundInterest()' method, then prints
the result of the calculation.
*/
public static void main(String[] args)
{

try
{
// Get a naming context
/* InitialContext jndiContext = new InitialContext();
System.out.println("Got context");*/

Properties props = new Properties();
props.putContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingC
ontextFactory");
//props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming
rg.jnp.interfaces");
props.put(Context.PROVIDER_URL, "localhost:1099");
InitialContext context=new InitialContext(props);

// Get a reference to the Interest Bean
Object ref = context.lookup("First");
System.out.println("Got reference");
// Get a reference from this to the Bean's Home interface
TestHome home = (TestHome)
PortableRemoteObject.narrow(ref, TestHome.class);
// Create an Interest object from the Home interface
TestRemote call = home.create();
System.out.println("Process strat");
String msg = call.process();
System.out.println(msg);
System.out.println("Process end");
}
catch(java.rmi.RemoteException re)
{
re.printStackTrace();
}
catch(Throwable t)
{
t.printStackTrace();
}
}
}

Help would be so appriciate,
Thanks,
Sharief
Nauman Khan
Ranch Hand

Joined: Aug 07, 2003
Posts: 49
Hi Ahmed,
I'm not sure which application server you are using right now,you have to set the classpath with the following jar files if you are using jboss.

.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\htmlconverter.jar;%JBOSS_HOME%\client\jboss-j2ee.jar;%JBOSS_HOME%\client\jbossall-client.jar;%JBOSS_HOME%\client\log4j.jar;%JBOSS_HOME%\client\jnp-client.jar;%JBOSS_HOME%\client\jboss-client.jar;%JBOSS_HOME%\client\jnet.jar;%JBOSS_HOME%\client\jboss-iiop-client.jar;%JBOSS_HOME%\client\jboss-common-client.jar
Hope this could help.
Regards,
Nauman
Ahamed Sharif
Ranch Hand

Joined: Aug 24, 2003
Posts: 47
Hi,
Thanks succefully i got the results, and i am using jboss3.0.8+tomcat4.24
Khuda hafiz,
Sharief
 
Consider Paul's rocket mass heater.
 
subject: NoInitialContextException
 
Similar Threads
error on running an ejb application
Naming Context problem
getting exception while bean look up by PortableRemoteObject.narrow
How to call ejb from jsp
exception error!!