I Can't create and add en entry in Ldap using Java

Hello there,

I'm pretty new to LDAP programming, and I have been trying to create and add an entry to a directory using the code sample provided in the JNDI tutorial, but for some reasons, I didn't manage.

the configuration file is well set up because I can't create, add, delete and modify just about anything I want from openLDAP command lines, but my real problem is to do it with java.

here is my code:

import java.util.Hashtable;

import javax.naming.ldap.*;

import javax.naming.directory.*;

import javax.naming.*;

import javax.net.ssl.*;

import java.io.*;

publicclass Newuser

{

publicstaticvoid main (String[] args)

{

//LDAPEntry myEntry = new LDAPEntry();

Hashtable<String, String> env =new Hashtable<String, String>(11);

String adminName ="CN=ldap_admin,o=JNDITutorial,dc=img,dc=org";

String adminPassword ="xxxxxx";

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://localhost:389/o=JNDITutorial,dc=img,dc=org");

// Create the initial directory context

//JNDILDAPConnectionManager jndiManager = new JNDILDAPConnectionManagerImpl();

try{

DirContext ctx =new InitialDirContext(env);

System.out.println("Connection to LDAP server done" );

final String groupDN ="ou=people,o=JNDITutorial,dc=img,dc=org";

//DirContext dirCtx = jndiManager.getLDAPDirContext();

People people =new People("Thiru","Thiru","Thiru Ganesh","Ramalingam","ou=people","thiru@jndiapi.com");

// The Common name must be equal in Attributes common Name

ctx.bind("cn=Thiru," + groupDN, people);

System.out.println("** Entry added **");

//jndiManager.disConnectLDAPConnection(ctx);

}catch(NamingException exception){

System.out.println("**** Error ****");

exception.printStackTrace();

System.exit(0);

}

}

}

adn this is the class People that i want to instantiate

import java.util.Hashtable;

import javax.naming.Binding;

import javax.naming.Context;

import javax.naming.Name;

import javax.naming.NameClassPair;

import javax.naming.NameParser;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;

import javax.naming.directory.*;

publicclass Peopleimplements DirContext{

public People(String uid,String cn,String givenname,String sn,String ou,String mail){

BasicAttributes myAttrs =new BasicAttributes(true);//Basic Attributes

Attribute objectClass =new BasicAttribute("objectclass");//Adding Object Classes

objectClass.add("inetOrgPerson");

/*objectClass.add("organizationalPerson");

objectClass.add("person");

objectClass.add("top");*/

Attribute ouSet =new BasicAttribute("ou");

ouSet.add("people");

ouSet.add(ou);

myAttrs.put(objectClass);

myAttrs.put(ouSet);

myAttrs.put("cn",cn);

myAttrs.put("sn",sn);

myAttrs.put("mail",mail);

}

}

as I said, I can add the new entry using an LDIF file "new.txt" that looks like this:

dn:cn=Hamido Saddo,ou=People,o=JNDITutorial,dc=img,dc=org

cn:Hamido Saddo

mail:samidou@aim.com

telephonenumber:3838393038703

sn:hamido

objectclass:top

objectclass:person

objectclass:organizationalPerson

objectclass:inetOrgPerson

using the following command:

ldapadd -D"cn=ldap_admin,o=JNDITutorial,dc=org,dc=img" -W -f new.txt

and eveything works

but when i try with the java, i get the following error:

javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - entry has no objectClass attribute]

so, can anyone help me please !!!

[6536 byte] By [samdanna] at [2007-11-27 8:58:25]
# 1

uncomment these lines

/*objectClass.add("organizationalPerson");

objectClass.add("person");

objectClass.add("top");*/

you need all of them to add successfully

your LDIF has all the four lines

objectclass:top

objectclass:person

objectclass:organizationalPerson

objectclass:inetOrgPerson

hope this helps

premSa at 2007-7-12 21:24:27 > top of Java-index,Core,Core APIs...
# 2
Hi Sandamn,Try omitting the operator and the dc from your DN structure. for eg.. "cn=Hamido Saddo,ou=People"try with this DN, It might work..Also add the object class value in the attribute list.
Fredy_Tridenta at 2007-7-12 21:24:27 > top of Java-index,Core,Core APIs...