Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: Need to specify class name in environment or system property LDAP and JNDI

Tags:

java

ldap

jndi

This is my code:

     public class TestConnection {

public static void main(String[] args) {
    String password = "s3cret";
    Map<String, String> env = new HashMap<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=userdev,dc=local");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    //env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +"cn=users"); // replace with user DN
    env.put(Context.SECURITY_PRINCIPAL, "cn=dcmanager,cn=users,dc=userdev,dc=local"); // replace with user DN
    env.put(Context.SECURITY_CREDENTIALS, password);

    DirContext ctx = null;
    try {
       ctx = new InitialDirContext();
    } catch (NamingException e) {
       // handle
    }
    try {
       SearchControls controls = new SearchControls();
       controls.setSearchScope( SearchControls.SUBTREE_SCOPE);
       ctx.search( "", "(objectclass=person)", controls);
       // no need to process the results
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        System.out.println("catch 1");
       // The base context was not found.
       // Just clean up and exit.
    } catch (NamingException e) {
        System.out.println("catch 2");
        e.printStackTrace();
       // exception handling
    } finally {
       // close ctx or do Java 7 try-with-resources http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
    }

}

    }

I got this error (catch 2) : javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

I have looked for a lot of solutions, but I don't get the error. Where is the problem? Maybe the context, but I think that the code is perfectly correct.

like image 835
user840718 Avatar asked Jun 19 '14 13:06

user840718


People also ask

How to set JNDI properties in Java?

You can set JNDI properties by creating a HashTable and populating it with the required properties using javax. naming. Context fields as keys and String objects as values. When you instantiate the initial context, pass the HashTable into the the initial context constructor.

What is JNDI used for?

The Java Naming and Directory Interface™ (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java™ programming language.

What is initial context in Java?

The initial context implements the Context interface and provides the starting point for resolution of names. When the initial context is constructed, its environment is initialized with properties defined in the environment parameter passed to the constructor, and in any application resource files.


1 Answers

You have to construct the InitialDirContext object using the env map you have populated. i.e. use the following code to construct it;

ctx = new InitialDirContext(env);

like image 84
Priyesh Avatar answered Sep 23 '22 15:09

Priyesh