Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:58:17+00:00 2026-05-26T21:58:17+00:00

I’ve got a windows server 2008R2 with an active directory. I want to create

  • 0

I’ve got a windows server 2008R2 with an active directory.
I want to create a Java program, what allows a 08/15-user to add new users to this AD.

I have found an [example][1] in the oracle forums and modified it for my AD.:

package model;

import java.io.IOException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.StartTlsRequest;
import javax.naming.ldap.StartTlsResponse;

public class NewUser2 {
public static void main(String[] args) {

    Hashtable<String,String> env = new Hashtable<String, String>();
    String adminName = "CN=Administrator,CN=Users,DC=Dom215-01,DC=local";
    String adminPassword = "g18";
    String userName = "CN=Foo Bar,OU=Schueler,DC=Dom215-01,DC=local";
    String groupName = "OU=Schueler,DC=Dom215-01,DC=local";

    env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");

    // set security credentials, note using simple cleartext authentication
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, adminName);
    env.put(Context.SECURITY_CREDENTIALS, adminPassword);

    // connect to my domain controller
    env.put(Context.PROVIDER_URL, "ldap://10.18.215.112");

    try {

        // Create the initial directory context
        LdapContext ctx = new InitialLdapContext(env, null);

        // Create attributes to be associated with the new user
        Attributes attrs = new BasicAttributes(true);

        attrs.put("objectClass", "Schueler");
        attrs.put("samAccountName", "FooBar");
        attrs.put("cn", "Foo Bar");

        // These are some optional (but useful) attributes
        attrs.put("givenName", "Foo");
        attrs.put("sn", "Bar");
        attrs.put("displayName", "Foo Bar");
        attrs.put("description", "Test Subject");

    /*  
        attrs.put("userPrincipalName", "asdf@asdf.com");
        attrs.put("mail", "sdaf@sdaf.com");
        attrs.put("telephoneNumber", "999 123 4567");
    */

        // some useful constants from lmaccess.h
        int UF_ACCOUNTDISABLE = 0x0002;
        int UF_PASSWD_NOTREQD = 0x0020;
        int UF_PASSWD_CANT_CHANGE = 0x0040;
        int UF_NORMAL_ACCOUNT = 0x0200;
        int UF_DONT_EXPIRE_PASSWD = 0x10000;
        int UF_PASSWORD_EXPIRED = 0x800000;

        attrs.put(
                "userAccountControl",
                Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD
                        + UF_PASSWORD_EXPIRED + UF_ACCOUNTDISABLE));

        // Create the context
        Context result = ctx.createSubcontext(userName, attrs);
        System.out.println("Created disabled account for: " + userName);

        StartTlsResponse tls = (StartTlsResponse) ctx
                .extendedOperation(new StartTlsRequest());
        tls.negotiate();

        ModificationItem[] mods = new ModificationItem[2];

        String newQuotedPassword = "\"Password2000\"";
        byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");

        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("unicodePwd", newUnicodePassword));
        mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userAccountControl",
                        Integer.toString(UF_NORMAL_ACCOUNT
                                + UF_PASSWORD_EXPIRED)));

        ctx.modifyAttributes(userName, mods);
        System.out.println("Set password & updated userccountControl");

        try {
            ModificationItem member[] = new ModificationItem[1];
            member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                    new BasicAttribute("member", userName));

            ctx.modifyAttributes(groupName, member);
            System.out.println("Added user to group: " + groupName);

        } catch (NamingException e) {
            System.err.println("Problem adding user to group: " + e);
        }

        tls.close();
        ctx.close();

        System.out.println("Successfully created User: " + userName);

    } catch (NamingException e) {
        System.err.println("Problem creating object: ");
        e.printStackTrace();
    }

    catch (IOException e) {
        System.err.println("Problem creating object: ");
        e.printStackTrace();
    }
  }
}

Everything looks Ok so far, but when it tries to create the result object in line 76 it crashes with an NoSuchAttributeException and LDAP error code 16 (no such attribute).
I’ve tried various modifications in the username string and the attributes, but nothing helped.

Does anyone have an idea why this error occurs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:58:18+00:00Added an answer on May 26, 2026 at 9:58 pm

    Ok. Sorry, I’m new to this forum.
    Anyway… Here the solution again :

    package model;
    
    import java.util.Hashtable;
    
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.naming.directory.Attribute;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.BasicAttribute;
    import javax.naming.directory.BasicAttributes;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;
    
    public class NewUser {
    
        private static final String DOMAIN_NAME = "Dom215-01";
        private static final String DOMAIN_ROOT = "DC=Dom215-01,DC=local";
        private static final String DOMAIN_URL = "ldap://10.18.215.112:389";
        private static final String ADMIN_NAME = "CN=Administrator,CN=Users,DC=Dom215-01,DC=local";
        private static final String ADMIN_PASS = "g18";
    
        private String userName, firstName, lastName, password, organisationUnit;
        private LdapContext context;
    
        public NewUser(String userName, String firstName, String lastName,
                        String password, String organisationUnit) {
    
            this.userName = userName;
            this.firstName = firstName;
            this.lastName = lastName;
            this.password = password;
            this.organisationUnit = organisationUnit;
    
            Hashtable<String, String> env = new Hashtable<String, String>();
    
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    
            // set security credentials, note using simple cleartext authentication
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, ADMIN_NAME);
            env.put(Context.SECURITY_CREDENTIALS, ADMIN_PASS);
    
            // connect to my domain controller
            env.put(Context.PROVIDER_URL, DOMAIN_URL);
    
            try {
                this.context = new InitialLdapContext(env, null);
            } catch (NamingException e) {
                System.err.println("Problem creating object: ");
                e.printStackTrace();
            }
    
        }
    
        public boolean addUser() throws NamingException {
    
            // Create a container set of attributes
            Attributes container = new BasicAttributes();
    
            // Create the objectclass to add
            Attribute objClasses = new BasicAttribute("objectClass");
            objClasses.add("top");
            objClasses.add("person");
            objClasses.add("organizationalPerson");
            objClasses.add("user");
    
            // Assign the username, first name, and last name
            String cnValue = new StringBuffer(firstName).append(" ").append(lastName).toString();
            Attribute cn = new BasicAttribute("cn", cnValue);
            Attribute sAMAccountName = new BasicAttribute("sAMAccountName", userName);
            Attribute principalName = new BasicAttribute("userPrincipalName", userName
                    + "@" + DOMAIN_NAME);
            Attribute givenName = new BasicAttribute("givenName", firstName);
            Attribute sn = new BasicAttribute("sn", lastName);
            Attribute uid = new BasicAttribute("uid", userName);
    
            // Add password
            Attribute userPassword = new BasicAttribute("userpassword", password);
    
            // Add these to the container
            container.put(objClasses);
            container.put(sAMAccountName);
            container.put(principalName);
            container.put(cn);
            container.put(sn);
            container.put(givenName);
            container.put(uid);
            container.put(userPassword);
    
            // Create the entry
            try {
                context.createSubcontext(getUserDN(cnValue, organisationUnit), container);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    
        private static String getUserDN(String aUsername, String aOU) {
            return "cn=" + aUsername + ",ou=" + aOU + "," + DOMAIN_ROOT;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.