title: Ldap modify Attribute Java
body: <p>I am going mad over a LDAP attribute modification.
  Be aware, I am very beginner so my code might contain mistakes and loads of issues but here is what I am trying to do.</p>
  <p>I need to change the following attribute of a AD user <code>givenName</code>, <code>samAccountName</code>, <code>displayName</code> to the one of my choice.
  I have 2 classes, one that searches for AD user (and works perfectly), and one that should modify the above attribute. However I keep getting error <code>InvocationTargetException</code></p>
  <p>This is Class 2, the one that does not work:</p>
  <pre class="lang-java prettyprint-override"><code>     public void modify() {
          try {
           
           
              //connecting to the domain 
              Hashtable&lt;String, String&gt; ldapEnv = new Hashtable&lt;String, String&gt;(11);
              ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY,&quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
              ldapEnv.put(Context.SECURITY_PROTOCOL,&quot;TLSv1.2&quot;);
              ldapEnv.put(Context.PROVIDER_URL,&quot;ldap://10.61.0.6:389&quot;);
              
              
              ldapEnv.put(Context.SECURITY_PRINCIPAL,&quot;CN=ldapadmin,OU=Users,OU=Dublin,OU=Europe,OU=Offices,DC=vmtech,DC=com&quot;);
              ldapEnv.put(Context.SECURITY_CREDENTIALS,&quot;Password1&quot;);
              InitialDirContext ldapContext = new InitialDirContext(ldapEnv);
              DirContext ctx = new InitialDirContext(ldapEnv);
              
           
           System.out.println(NameObject);
              ModificationItem[] mods = new ModificationItem[3];
              //replace the GivenName
              Attribute A0 = new BasicAttribute(&quot;givenName&quot;, modgivenName);
              mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, A0);
              // replace the sAMAccountName
              Attribute A1 = new BasicAttribute(&quot;samAccountName&quot;, ModsAMAccountName); 
              mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, A1); 
              // Replace the displayname
              Attribute A2 = new BasicAttribute(&quot;displayName&quot;,ModdisplayName);
              mods[2] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, A2);
              
                       for(int i =0; i &lt;= mods.length; i++){ 
                  System.out.println(mods[i]);
                  
              }
              // apply the modification to the object
              ctx.modifyAttributes(NameObject, mods);
  
                     
       
      } 
       
           catch (NamingException ex) {
              Logger.getLogger(LdapSearch.class.getName()).log(Level.SEVERE, null, ex);
          }
           
       }
  </code></pre>