views:

80

answers:

3

Hi,

I have a situation where I have to load a named class. If there are multiple classes with the same name (say, com.example.myclass) in my class path I have to load both. I am using 'loadClass()' method of my CustomLoader class which derives from java.lang.ClassLoader. I have not changed the behaviour of the parent class but am simply calling the parent's methods. My problem is that if there are two classes with the same name, I am able to load only one of them. I have scanned the web for a solution but haven't found any. I found many solutions on reloading classes by creating a new class loader instance but in my case the new instance will probably end up loading the first class again. Can this problem be solved?

EDIT: I forgot to mention that the two classes with the same name are in different jar files.

EDIT: Both Jon and Stephen gave the same solution but I can mark only one as answer. Sorry :(. I have up voted both answers though.

+4  A: 

I believe the JVM and class libraries assume that a class name is unique within a classloader. Therefore if you want to load the same class name for different classes, you'll need different classloaders (e.g. one for each jar file).

Using them could be tricky, but that's the sort of problem you get with this sort of thing.

Jon Skeet
I am new to Java. Can you please explain what you mean by one classloader for each jar? How do I specify a different class loader for each jar?
Rajorshi
@Rajorshi: You would have to create a new instance of (say) URLClassLoader referring to each jar file, and ask each instance to load the class. But to be honest, if you're new to Java I would start attempting this sort of thing for a while. I'd even suggest that an experienced Java developer should try to avoid getting into this situation if possible.
Jon Skeet
@Jon: Thanks for the tip. I can't really refuse to implement this module but senior java developers will be reviewing the code. If I miss anything they should be able to detect it.
Rajorshi
@Rajorshi: Okay. It really depends what you need to do with these classes afterwards, but if you're doing anything particularly complicated this could get messy.
Jon Skeet
+3  A: 

The classloader API has no way to specify which of the two classes with the same name that your application is trying to load.

You will need to configure two different classloaders with different classpaths. Then you will need to load the classes by calling the loadClass(String) method on the respective classloaders.

There are a number of "gotchas" with doing this kind of thing. One of them is that the types of the instances of the respective classes will be different, and typecasts from one type to the other will fail.

Stephen C
Unfortunately, it is not possible to configure class paths at runtime.
Rajorshi
@Rajorshi - oh yes it is! See http://download.oracle.com/javase/6/docs/api/java/net/URLClassLoader.html
Stephen C
A: 

I suggest that there is something seriously wrong with the problem here. How can such a state of affairs possibly come about in the first place? There is something seriously wrong with a development process that can produce duplicate classes in the first place.

Or is that the problem that this is trying to solve? Or diagnose?

EJP
It is a plugin framework that expects all third party to be present in a particular folder. Plugin can be written by anybody and so we can not guarantee that there will be no clashes.
Rajorshi
Then I suggest you make a rule that they use com.mycompany.whatever in their package names as recommended for all Java programming, and disclaim responsibility if they don't observe it.
EJP