Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Cannot instantiate class : LdapCtxFactory

Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone help me in resolving the following issue. I am looking at a web server upgrad project and have run into difficulty with LDAP and Servlets part of the code.

WebServer: SunOne 6.0 SP8
Platform : Solaris 8

LDAP: Netscape directory server
Platform: Solaris 2.6

JRE 1.2

Following lines is used to initlise Directory Context

Hashtable env = new Hashtable(5, 0.75F);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, getProviderUrl());
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + getSecurityPrincipal());
env.put(Context.SECURITY_CREDENTIALS, getSecurityCredentials());

DirContext ctx = new InitialDirContext(env);

The system throws the following exception at runtime

"Cannot instantiate class: com.sun.jndi.ldap.LdapCtxFactory"

com.sun.jndi.ldap.LdapCtxFactory is present in a jar called ldap.jar which i have specifed in my classpath. Also set the nobody as the file owner and its prviligies to 777

Using truss i can confirm that the webserver does resolve and get a filehandle to the ldap.jar file.

has anyone come across a similar problem where the class could not be initiated despite being present in the classpath.

I have also tried using
Thread.currentThread().setContextClassLoader(urlCl) withoug much luck.

Appricate if you could provide some answers.

Thanks in Advance.
Posts: 43016
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding not being able to instantiate a class although it is in the classpath: this might point to a problem occurring in a static initializer or the constructor. Have you run it through a debugger to see if it get to those places, and whether it makes it through?
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not 100% sure, but I assume the Problem is JRE 1.2.

Class javax.naming.ldap.InitialLdapContext is avaliable since 1.3, and the same is true for interface javax.naming.ldap.LdapContext. These types are probably needed if your com.sun.jndi.ldap.LdapCtxFactory tries to load.

Some options that may work:
1) upgrade your JRE
2) use existing JRE but install a new version of JNDI

I hope this helps,
sandesh bangera
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for your reply. In this case, the problem was due to the differnt classloaders invovled, that has been introduced in JDK2 onwards.

However, did the following to fix my problem.

//Retrive and store previous Class context.
ClassLoader prevCl = Thread.currentThread().getContextClassLoader();


// Get the current classes Class loader info
ClassLoader classcl = this.getClass().getClassLoader();

//Set class context loader to the current thread

Hashtable env = new Hashtable(5, 0.75F);

DirContext ctx = new InitialDirContext(env)

}catch(Exception e) {
} finally {
// Restore

Thanks once again.
    Bookmark Topic Watch Topic
  • New Topic