cancel
Showing results for 
Search instead for 
Did you mean: 

problem in LDAP search

Former Member
0 Kudos

I am having problem in running the below code, can someone pls help me out with it ? It either doesn't return anything or gives error code 32.

import javax.naming.*;

import javax.naming.ldap.*;

import java.util.*;

import javax.naming.directory.*;

public class ldapdemo {

public static void main(String[] args) {

try {

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, "LDAP://dewdfx1b1:389");

LdapContext ldapctx = new InitialLdapContext(env, null);

Attributes matchAttrs = new BasicAttributes();

matchAttrs.put("uid","I028949");

String [] attrIDs = {"cn","mail"};

NamingEnumeration answer = ldapctx.search("o=SAP", matchAttrs, attrIDs);

while(answer.hasMore()) {

SearchResult sr = (SearchResult)answer.next();

Attributes resultAttrs = sr.getAttributes();

String username = resultAttrs.get("cn").getID();

String mail = resultAttrs.get("mail").getID();

System.out.println("username="+username);

System.out.println("mail="+mail);

}

} catch (NamingException e) {

System.out.println(e.getMessage());

return;

}

}

}

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Sameer,

First, I see no authentication in your coding. Probably it is required.

Second, where is base DN for binding?

Try this sample (for InitialContext info with credentials):

http://www.sgi.nu/diary/2005/10/27/accessing-the-global-address-list-in-ms-exchange-from-java/

Also I highly recommend to download Java LDAP browser/editor and try to perform your program task it:

http://www-unix.mcs.anl.gov/~gawor/ldap/

Valery Silaev

EPAM Systems

http://www.NetWeaverTeam.com