File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Java Products and Servers and the fly likes Cannot instantiate class: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "Cannot instantiate class: " Watch "Cannot instantiate class: " New topic

Cannot instantiate class:

sandesh bangera

Joined: Nov 13, 2002
Posts: 12

this problem could be more to do with the web server than servlets itself.

WebServer: SunOne 6.0 SP8
Platform : Solaris 8

LDAP: Netscape directory server
Platform: Solaris 2.6

Got a problem with a servlet that interacts with a LDAP server. I use the following line to initlise Directory Context

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

however, 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.

Appricate if you could provide some answers.

Mods: In case this topic has been answered before, could you point me to the right forum please.

Thanks & Regards
[ January 13, 2006: Message edited by: sandesh bangera ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Applications running inside servlet containers don't use the classpath, ignoring it allows a single container to run multiple applications without their code bases interfering with each other.

I'll move this to the Sun One forum, but basically you should put ldap.jar in the lib directory for that domain.
sandesh bangera

Joined: Nov 13, 2002
Posts: 12
Thanks David.

I will try this out first thing on Monday.
sandesh bangera

Joined: Nov 13, 2002
Posts: 12
Fixed this. this is due to the different class loaders introduced in JDK 2 onwards. Did hte 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

Mods. This thread can be closed now.

I agree. Here's the link:
subject: Cannot instantiate class:
It's not a secret anymore!