This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NoInitialContextException" Watch "NoInitialContextException" New topic
Author

NoInitialContextException

John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Hello:

I get the following stack trace:

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(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:87)
at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:210)
at com.enetrix.sis.jaas.LDAPLoginModule.validate(LDAPLoginModule.java:387)
at com.enetrix.sis.jaas.LDAPLoginModule.login(LDAPLoginModule.java:132)
. . .


Here is my code:

DirContext authContext = null;



Hashtable authEnv = new Hashtable();
String dn = user;

authEnv.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
authEnv.put(Context.PROVIDER_URL, "ldap://192.168.132.20:389");
authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
authEnv.put(Context.SECURITY_PRINCIPAL, dn);
authEnv.put(Context.SECURITY_CREDENTIALS, password);

try {
authContext = new InitialDirContext(authEnv);
if (debug) {
logger.debug("LDAP authentication passed.");
}
authContext.close();
}
catch (AuthenticationException authEx) {
if (debug) {
logger.debug("LDAP authentication failed.");
}
throw new LoginException("Invalid Username or Password");
}
catch (NamingException namEx) {
if (debug) {
logger.debug("JNDI error");
}
throw new LoginException("JNDI error");
}

Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("samAccountName", dn));

// ERROR OCCURS HERE:
NamingEnumeration answer = authContext.search("DC=Enetrix", matchAttrs);

while (answer.hasMore()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());

}



Any ideas?
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Never mind. I was closing my context!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NoInitialContextException