title: javax.naming.NoInitialContextException: Need to specify class name in environment or sys
body: <p>I have this error when I run my Java program.  The thing that I want to do is to be able to create an LDAP user with Java code.  My goal is to be able to add, remove, set password and add the user in an existing group.  </p>
  
  <p>The better thing is to have an input box also</p>
  
  <p>Thanks</p>
  
  <pre><code>import javax.naming.directory.DirContext;
  
  import javax.naming.directory.InitialDirContext;
  import javax.naming.directory.Attributes;
  import javax.naming.directory.BasicAttributes;
  import javax.naming.directory.BasicAttribute;
  import javax.naming.ldap.InitialLdapContext;
  import javax.naming.ldap.LdapContext;
  import javax.naming.NamingException;
  import java.util.Hashtable;
  import java.util.Properties;
  
  
  
  public class LdapExampleAdd {
  
  
  private static LdapContext ctx;
  
  public static void main(String[] args) 
  {
  
  //Identify service provider to use
  
  Hashtable env = new Hashtable();
  Properties prop = new Properties();  
  prop.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");  
  prop.put("java.naming.provider.url", "ldap://ldapserverip:389");  
  prop.put("java.naming.security.principal", "cn=Administrator,cn=Users,dc=domain,dc=local");  
  prop.put("java.naming.security.credentials", "password"); 
  
  try {  
      setCtx(new InitialLdapContext(prop, null));  
      System.out.println("Connected"); 
  
    }  
    catch (NamingException ex) { 
      System.err.println("Not connected");     
      System.exit(0);
    }  
  
  try 
  { 
  // Create the initial directory context
  InitialDirContext initialContext = new InitialDirContext(env);
  
  
  
  DirContext dCtx = (DirContext)initialContext;
  
  Attributes matchAttrs = new BasicAttributes(true);
  matchAttrs.put(new BasicAttribute("uid", "defaultuser"));
  matchAttrs.put(new BasicAttribute("cn", "defaultuser")); 
  matchAttrs.put(new BasicAttribute("givenname", "defaultuser")); 
  matchAttrs.put(new BasicAttribute("sn", "defaultuser")); 
  matchAttrs.put(new BasicAttribute("userpassword", "password")); 
  matchAttrs.put(new BasicAttribute("objectclass", "top")); 
  matchAttrs.put(new BasicAttribute("objectclass", "person")); 
  matchAttrs.put(new BasicAttribute("objectclass", "organizationalPerson")); 
  matchAttrs.put(new BasicAttribute("objectclass","inetorgperson")); 
  String name="uid=defaultuser";
  
  InitialDirContext iniDirContext = (InitialDirContext)dCtx;
  iniDirContext.bind(name,dCtx,matchAttrs);
  
  iniDirContext.close();
  dCtx.close(); 
  } 
  catch (NamingException ne) 
  {
  System.err.println(ne);
  }
  catch(Exception e)
  {
  System.err.println(e);
  }
  }
  
  public static LdapContext getCtx() {
      return ctx;
  }
  
  public static void setCtx(LdapContext ctx) {
      LdapExampleAdd.ctx = ctx;
  }
  }
  </code></pre>
  
  <p>Who will be the real expert</p>