In this post under Java LDAP, I will show with example how to retrieve an attribute of an existing ldap entry.
For our example we will take the below ldap entry.

Please note in the above screenshot, all the attributes have only one value except attribute “telephoneNumber” which has 2 values.
Below is the complete main class for your reference.
Main class
1 package package8;
2
3 import javax.naming.Context;
4 import javax.naming.NamingEnumeration;
5 import javax.naming.NamingException;
6 import javax.naming.directory.*;
7 import java.util.Hashtable;
8
9 public class LDAPDemo8 {
10 public static void main(String[] args) {
11 Hashtable<String, Object> env = new Hashtable<String, Object>();
12 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
13 env.put(Context.PROVIDER_URL, "ldap://localhost:1389");
14 env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=org");
15 env.put(Context.SECURITY_CREDENTIALS, "adminpassword");
16 DirContext ctx = null;
17 try {
18 ctx = new InitialDirContext(env);
19 String dn = "cn=user2,ou=tester,dc=example,dc=org";
20 Attributes attributes = ctx.getAttributes(dn);
21 NamingEnumeration<BasicAttribute> basicAttributeNamingEnumeration = (NamingEnumeration<BasicAttribute>) attributes.getAll();
22 while (basicAttributeNamingEnumeration.hasMoreElements()) {
23 BasicAttribute basicAttribute = basicAttributeNamingEnumeration.nextElement();
24 if(basicAttribute.size() == 1) {
25 System.out.println(basicAttribute.getID() + ":" + basicAttribute.get());
26 } else {
27 NamingEnumeration namingEnumeration = basicAttribute.getAll();
28 while(namingEnumeration.hasMoreElements()) {
29 System.out.println(basicAttribute.getID() + ":" + namingEnumeration.nextElement());
30 }
31 }
32 }
33 } catch(NamingException excep) {
34 excep.printStackTrace();
35 } finally {
36 if(ctx != null) {
37 try {
38 ctx.close();
39 } catch(NamingException excep) {
40 excep.printStackTrace();
41 }
42 }
43 }
44 }
45 }
In the above code, at line 18, I create an instance of “InitialDirContext” class.
At line 19, I set the dn of the ldap entry.
At line 20, I call “getAttributes” method available on “DirContext” instance passing the dn of the ldap entry as a method argument.
The “getAttributes” method returns all the attributes of an ldap entry
At line 21, I get all the attributes in the form of an enumeration named “basicAttributeNamingEnumeration”.
In the loop from 22 to 32, I will loop through each element of the enumeration “basicAttributeNamingEnumeration”.
Inside the loop line 23, I get each attribute and assign it to “BasicAttribute” instance named “basicAttribute”.
At line 24, I check if the attribute “basicAttribute” has more than one values.
If no, I will go to line 25 and print the attribute id and its value by calling “get” method on the “basicAttribute” instance.
If yes, I will go to line 27, get all the values of the current attribute in the loop as an enumeration and assign it to “namingEnumeration” variable.
In the loop 28 to 30 I loop through the values of “namingEnumeration” and print them to console along with the attribute id.
In this way we can get the attributes of an ldap entry.
Below is the output for your reference
Output
telephoneNumber:1111111111
telephoneNumber:2222222222
description:user2 tester description
objectClass:person
objectClass:top
sn:user2
cn:user2