title: LDAPContext.search() returns empty result
body: <p>Using LDAPContext class I search for a specific user and try to get whether it exists. But <code>search()</code> method returns an empty response.</p>
  
  <pre><code>private int checkUserOnLDAP() {
  
      String strLDAPServer = "ldap://ldap.forumsys.com:389";
      String strLDAPPricipal = "cn=read-only-admin,dc=example,dc=com";
      String strPassword = "password";
      String strSearchBase = "ou=mathematicians,dc=example,dc=com";
      String strUserToSearch = "riemann";
  
      Hashtable&lt;String, String&gt; environment = new Hashtable&lt;String, String&gt;();
      environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
      environment.put(Context.PROVIDER_URL, strLDAPServer);
      environment.put(Context.SECURITY_AUTHENTICATION, "simple");
      environment.put(Context.SECURITY_PRINCIPAL, strLDAPPricipal);
      environment.put(Context.SECURITY_CREDENTIALS, strPassword);
  
      LdapContext ctxGC = null;
      try {
          ctxGC = new InitialLdapContext(environment, null);
          ctxGC.getAttributes("");
      } catch (NamingException e) {
          System.err.print("SEARCHER BLOCKED USER");
          e.printStackTrace();
      } catch (Exception e) {
          System.err.print("SEARCHER WRONG PASSWORD");
          e.printStackTrace();
      }
  
      System.out.println("SEARCHER LOGIN SUCCESSFUL");
  
      System.out.println("NOW TRYING TO SEARCH");
      try {
          String searchFilter = "(&amp;(objectClass=user)(sAMAccountName=" + strUserToSearch + "))";
          String returnedAtts[] = new String[0];
          SearchControls searchCtls = new SearchControls();
          searchCtls.setReturningAttributes(returnedAtts);
          searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
          NamingEnumeration&lt;?&gt; answer = ctxGC.search(strSearchBase, searchFilter, searchCtls);
  
          if (answer.hasMoreElements()) {
              Object a = answer.nextElement();
              System.out.println("SUCCESFULLY, FOUND USER");
              return 0;
          } else {
              System.out.println("ANSWER HAS NO ELEMENTS");
          }
      } catch (Exception e) {
          System.out.println("SEARCH FAILED");
          e.printStackTrace();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>While testing, I use an online ldap service: <a href="http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/" rel="nofollow noreferrer">http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/</a> </p>
  
  <p>Considering this online test service how can I check whether user exists?</p>