Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot instantiate class : LdapCtxFactory

 
sandesh bangera
Greenhorn
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.
Sandesh
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • 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?
 
Stefan Zoerner
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sandesh bangera
Greenhorn
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();

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic