Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoInitialContextException thrown

Douglas Kent
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to access a simple HelloWorld EJB (stateless session) deployed to a localhost Sun One Java Application server versions 8. Here's what I'm getting:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at MySessionEJBClient.main(


if (NamingManager.hasInitialContextFactoryBuilder()) System.out.println("Has Factory Builder set");
InitialContext ctx = new InitialContext();
System.out.println ("After new InitialContext");
java.lang.Object ref = ctx.lookup("java:comp/env/ejb/MySessionEJBHome"); (throws exception here..)

My question is very simple: What class should I use to set the property "java.naming.factory.initial", which is described as the property that should have the name of the context factory class? Can't find it anywhere. Also, "NamingManager.hasInitialContextFactoryBuilder())" call is returning false, so it's clear that the property is not set.

I believe it's in the package defined by "com.sun.jndi", but cannot find any javadoc on this..

Thanks in advance,
Amit Jaipaul
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Douglas,

Please try to create your Context Object as follows:
// Set initial context and URL properties
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, "iiop://<host_name>:<port>");

// Create initial context and lookup
Context initial = new InitialContext(env);
Object obj = initial.lookup("java:comp/env/ejb/MySessionEJBHome"); (If you are accessing ur bean from within the app server)
Object obj = initial.lookup("MySessionEJBHome"); (If you are trying to access your bean from outside ur app server)
MySessionEJBHome home = (MySessionEJBHome)

I hope the above should solve ur problem.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic