This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread 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 Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NoInitialContextException" Watch "NoInitialContextException" New topic


John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230

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(
at javax.naming.InitialContext.getDefaultInitCtx(
at javax.naming.InitialContext.getURLOrDefaultInitCtx(
at com.enetrix.sis.jaas.LDAPLoginModule.validate(
at com.enetrix.sis.jaas.LDAPLoginModule.login(
. . .

Here is my code:

DirContext authContext = null;

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

authEnv.put(Context.PROVIDER_URL, "ldap://");
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.");
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));

NamingEnumeration answer ="DC=Enetrix", matchAttrs);

while (answer.hasMore()) {
SearchResult sr = (SearchResult);
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!
I agree. Here's the link:
subject: NoInitialContextException
It's not a secret anymore!