title: List all the users in the Active Directory Group
body: <p>I am facing some difficulties while listing all the users in the Active Directory. I reached to the group, but unfortunately, I could not retrieve all the users. I am looking for the user Full Names, usernames, directorate. My code is:</p>
  
  <pre><code>package client;
  
  import java.util.Hashtable;
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.naming.ldap.InitialLdapContext;
  import javax.naming.ldap.LdapContext;
  import javax.naming.ldap.*;   
  import java.util.Enumeration;
  
  import javax.naming.NamingEnumeration;
  import javax.naming.directory.DirContext;
  import javax.naming.directory.InitialDirContext;
  import javax.naming.directory.SearchControls;
  import javax.naming.directory.SearchResult;
  import javax.naming.directory.*; 
  
  public class AD1 {
      public AD1() {
          super();
      }
  
      public static String usersContainer = "cn=XX,ou=XX,ou=Groups,dc=XX,dc=XX,dc=XXX";
  
      public static void main(String[] args) {
  
          try {
              LdapContext ctx = null;
              Hashtable env = new Hashtable();
              env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
              env.put(Context.SECURITY_AUTHENTICATION, "Simple");
              //it can be &lt;domain\\userid&gt; something that you use for windows login
              //it can also be
              env.put(Context.SECURITY_PRINCIPAL, "username");
              // env.put(Context.SECURITY_CREDENTIALS, "password");
              try {
                  env.put(Context.SECURITY_CREDENTIALS, "password".getBytes("UTF8"));
              }
              catch (java.io.UnsupportedEncodingException e) { /* ignore */ }
  
              //in following property we specify ldap protocol and connection url.
              //generally the port is 389
              env.put(Context.PROVIDER_URL, "ldap://IP:PORT");
  
              ctx = new InitialLdapContext(env, null);
  
              System.out.println("Connection Successful.");
  
              DirContext ctx1 = new InitialDirContext(env);   
              SearchControls ctls = new SearchControls();        
              String[] attrIDs = { "distinguishedName","cn","name","uid",
                                   "sn",
                                   "givenname",
                                   "memberOf",
                                   "samaccountname",
                                   "userPrincipalName" };
  
              ctls.setReturningAttributes(attrIDs);       
              ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
              //ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
              NamingEnumeration answer = ctx1.search( usersContainer, "(objectClass=user)", ctls );     
  
              System.out.print("Out while loop");
              while(answer.hasMore()) {
                  System.out.print("while loop11");
                  SearchResult rslt = (SearchResult)answer.next();          
                  Attributes attrs = rslt.getAttributes();           
                  System.out.println(attrs.get("uid"));         
                  ctx1.close(); 
              }
  
          } catch(NamingException nex) {
              System.out.println("Active Directory Connection: FAILED");
              nex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Can anyone help.</p>