title: Active directory Change password - Java
body: <p>I am trying to change the password of Active directory user and get error code 65. Please see below the source code I used.</p>
  
  <pre><code>public class ADConnection {
      DirContext ldapContext;
      String baseName = ",CN=Users,DC=wso2,DC=test";
  
      public ADConnection() {
          try {
              Hashtable ldapEnv = new Hashtable(11);
              ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY,
                      "com.sun.jndi.ldap.LdapCtxFactory");
              ldapEnv.put(Context.PROVIDER_URL, "ldaps://192.168.18.xx:636");
              ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
              ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrator"
                      + baseName);
              ldapEnv.put(Context.SECURITY_CREDENTIALS, "xxxxxx");
              ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl");
              ldapContext = new InitialDirContext(ldapEnv);
          } catch (Exception e) {
              System.out.println(" bind error: " + e);
              e.printStackTrace();
              System.exit(-1);
          }
      }
  
      public void updatePassword(String username, String password) {
          try {
              String quotedPassword = "\"" + password + "\"";
              char unicodePwd[] = quotedPassword.toCharArray();
              byte pwdArray[] = new byte[unicodePwd.length * 2];
              for (int i = 0; i &lt; unicodePwd.length; i++) {
                  pwdArray[i * 2 + 1] = (byte) (unicodePwd[i] &gt;&gt;&gt; 8);
                  pwdArray[i * 2 + 0] = (byte) (unicodePwd[i] &amp; 0xff);
              }
              ModificationItem[] mods = new ModificationItem[1];
              mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                      new BasicAttribute("UnicodePwd", pwdArray));
              ldapContext.modifyAttributes("cn=" + username + baseName, mods);
              //ldapContext.modifyAttributes("cn=" + username, mods);
          } catch (Exception e) {
              System.out.println("update password error: " + e);
              System.exit(-1);
          }
      }
  
      public static void main(String[] args) {
          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
          // the keystore that holds trusted root certificates
          System.setProperty("javax.net.ssl.trustStore", "client-truststore.jks");
          //System.setProperty("javax.net.debug", "all");
          ADConnection adc = new ADConnection();
          adc.updatePassword("admin", "pass@word3");
      }
  }
  </code></pre>
  
  <p>I used the code from following link.
  <a href="http://blogs.msdn.com/b/alextch/archive/2012/05/15/how-to-set-active-directory-password-from-java-application.aspx" rel="nofollow">http://blogs.msdn.com/b/alextch/archive/2012/05/15/how-to-set-active-directory-password-from-java-application.aspx</a>
  What is the wrong with this ?</p>
  
  <p>Thanks</p>