Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:24:31+00:00 2026-06-10T06:24:31+00:00

I am able to run following search query successfully on openldap commandline tool: ldapsearch

  • 0

I am able to run following search query successfully on openldap commandline tool:

ldapsearch -h 1.11.1.1 -b "DC=ff2,DC=in" -s subtree -D "CN=Ldap Bind,OU=Service Accounts,OU=BA,DC=ff2,DC=in" -w G00Pass# sBAAccountName=testAccount

Now I have to execute it in java class. I have done the following:

Hashtable env = new Hashtable();

    env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, "ldap://1.11.1.1:389");
    env.put(Context.SECURITY_PRINCIPAL, "CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=ff2,DC=in");
    env.put(Context.SECURITY_CREDENTIALS, "H00Pass#");

    LdapContext context = new InitialLdapContext(env, null);
    // To get only 1000 results at a time.
    context.setRequestControls(
        new Control[]{new PagedResultsControl(1000, Control.CRITICAL)});

    String[] attrs={"CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=ff2,DC=in"};


    String base = "DC=ff2,DC=in";
    String filter = "(&(objectClass=user)(sAMAccountName=testAccount))";
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setReturningAttributes(attrs);
    SearchResult searchResults;
        NamingEnumeration<SearchResult> results =  context.search(base, filter, controls);
        if (results.hasMoreElements()) {
            SearchResult searchResult = (SearchResult) results.nextElement();
            if(results.hasMoreElements()){
                System.err.println("Matched multiple groups for the group with SID: ");
        }else{

            System.out.println( (String)searchResult.getAttributes().get("sAMAccountName").get());
        }
       }

This is giving me Null Pointer Exception at searchResult.getAttributes(). Here I am not sure how to include sBAAccountName filter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:24:33+00:00Added an answer on June 10, 2026 at 6:24 am

    You have to search with that criteria as follows:

    env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, "<LDAP HOST>");
    env.put(Context.SECURITY_PRINCIPAL, "<LDAP USER LOGIN>");
    env.put(Context.SECURITY_CREDENTIALS, "<LDAP USER PASSWORD>");
    
    LdapContext context = new InitialLdapContext(env);
    // To get only 1000 results at a time.
    context.setRequestControls(
        new Control[]{new PagedResultsControl(1000, Control.CRITICAL))});
    
    String attrs = "<List of attrs to be retrieved for each matching LDAP entry>";
    String base = "<Base of the search tree>";
    String filter = "<Your filter>";
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setReturningAttributes(attrs);
    SearchResults searchResults;
    do {
        searchResults = ctx.search(base, filter, controls);
        while (searchResults.hasMoreElements()) {
            // Process result.
        }
        // Process response controls to get the cookie 
        // and keep searching until it is null.
    }
    while (cookie is not null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following query, is it possible to be able to run
I'm able to run the following curl command (at the command line) successfully: curl
I am not able to run a simple select query with a where clause.
I need to be able to run an Oracle query which goes to insert
I am currently having the problem of not able to run the following code
Which databse must I install to be able to run examples in the following
I need to be able to run the following commands on a rooted Android
Using the following method I am able to run a transaction against an Oracle
I have been able to run my C# .net 3.5 app just fine on
Suddenly I am not able to run my application on an emulator or device

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.