File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes Cannot instantiate class : LdapCtxFactory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "Cannot instantiate class : LdapCtxFactory" Watch "Cannot instantiate class : LdapCtxFactory" New topic

Cannot instantiate class : LdapCtxFactory

sandesh bangera

Joined: Nov 13, 2002
Posts: 12
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.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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?
Stefan Zoerner
Ranch Hand

Joined: Nov 29, 2001
Posts: 121

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,

Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
sandesh bangera

Joined: Nov 13, 2002
Posts: 12
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.
I agree. Here's the link:
subject: Cannot instantiate class : LdapCtxFactory
It's not a secret anymore!