java - ERROR: Need to specify class name in environment or system property LDAP and JNDI -
this 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 user dn env.put(context.security_principal, "cn=dcmanager,cn=users,dc=userdev,dc=local"); // replace user dn env.put(context.security_credentials, password); dircontext ctx = null; seek { ctx = new initialdircontext(); } grab (namingexception e) { // handle } seek { searchcontrols controls = new searchcontrols(); controls.setsearchscope( searchcontrols.subtree_scope); ctx.search( "", "(objectclass=person)", controls); // no need process results } grab (namenotfoundexception e) { e.printstacktrace(); system.out.println("catch 1"); // base of operations context not found. // clean , exit. } grab (namingexception e) { system.out.println("catch 2"); e.printstacktrace(); // exception handling } { // close ctx or java 7 try-with-resources http://docs.oracle.com/javase/tutorial/essential/exceptions/tryresourceclose.html } } }
i got error (catch 2) : javax.naming.noinitialcontextexception: need specify class name in environment or scheme property, or applet parameter, or in application resource file: java.naming.factory.initial
i have looked lot of solutions, don't error. problem? maybe context, think code correct.
you have build initialdircontext object using env map have populated. i.e. utilize next code build it;
ctx = new initialdircontext(env);
java ldap jndi
No comments:
Post a Comment