Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;

public class Main {
    public static void main(String[] argv) throws Exception {
        Attributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(new BasicAttribute("sn", "YourName"));
        matchAttrs.put(new BasicAttribute("mail"));

        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "yourURL");

        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration e = ctx.search("ou=People", matchAttrs);

        if (e.hasMore()) {
            SearchResult entry = (SearchResult) e.next();
            // Abandon rest of results
            e.close();
        }
    }
}