FreeKB - How to connect to an LDAP server is Java Eclipse
How to connect to an LDAP server is Java Eclipse

Home > Search > How-to


Use this markup in a class to make a connection to an LDAP server.

package com.example;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.ldap.InitialLdapContext;

public class ldapDemo {

  public static void main(String[] args) throws Exception {

    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://example.com:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "your_username");
    env.put(Context.SECURITY_CREDENTIALS, "your_password");

    try {
      new InitialLdapContext(env, null);  
      System.out.println("Successfully connected to LDAP");
 
    } 

    catch (AuthenticationException ex) {
         System.out.println("Failed - Unable to connect to LDAP");
          ex.printStackTrace();
    }  
  }
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 8b5bb in the box below so that we can be sure you are a human.




Comments