views:

319

answers:

2

Hi,

I am trying following

Context ctx = (Context) jndiCntx.lookup(fSTANDARD_ENVIRONMENT);

Object obj = ctx.lookup(fSTANDARD_JNDINAME);

And following code is returning me false

MyClass.class.isAssignableFrom(obj.getClass())

although

MyClass.class.getName().equalsIgnoreCase(obj.getClass().getName()) returns true.

I am not able to cast obj to MyClass as it throws ClassCastException.

What could be the issue?

+1  A: 

My guess is that the class has been loaded by two different classloaders.

Look at obj.getClass().getClassLoader() vs calling getClassLoader() in your current code.

Is the class itself available from two different jar files? That's normally an easy one to sort out. It's harder if you've got two separate classloaders which both use the same jar file.

What container are you running in? I suggest you look at the container-specific documentation for classloaders... for example, here's the Tomcat 5.5 ClassLoader how-to.

Jon Skeet
Yes I am getting 2 classloaders. But one is parent to other.
sonam
Okay, so it sounds like the child classloader isn't delegating to its parent. You probably need to work out why that's happening, and that could solve it. This will be specific to your exact situation though, I suspect.
Jon Skeet
There's also the possibility that a *context classloader* would help - I've never understood them myself :)
Jon Skeet
yep. will check the same. thanks a lot.
sonam
This sounds good, but as far as I can tell from the javadocs class.isAssignableFrom is checking by the class type- are you sure that will take the different classloader into account?
Steve B.
@SteveB: Yes - the same class name loaded from a different class loader is a different class as far as everything like that is concerned.
Jon Skeet
A: 

hang on, why are you calling "equalsIgnoreCase" to check the classnames rather than just plain "equals"? Is it possible you've got a conflict between (nearly) matching names?

Steve B.