File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Cannot instantiate class : LdapCtxFactory

sandesh bangera
Greenhorn

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.
Sandesh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
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?


Ping & DNS - my free Android networking tools app
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Hi,

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
http://java.sun.com/products/jndi/downloads/index.html

I hope this helps,
Stefan


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

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();

try{

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

//Set class context loader to the current thread
Thread.currentThread().setContextClassLoader(classcl);

Hashtable env = new Hashtable(5, 0.75F);
env.put(Context.INITIAL_CONTEXT_FACTORY,.....)
....

DirContext ctx = new InitialDirContext(env)

}catch(Exception e) {
....
} finally {
// Restore
Thread.currentThread().setContextClassLoader(prevCl);
}

Thanks once again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot instantiate class : LdapCtxFactory