GeeCON Prague 2014*
The moose likes Other JSE/JEE APIs and the fly likes Multiple class  loading issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Multiple class  loading issue" Watch "Multiple class  loading issue" New topic
Author

Multiple class loading issue

swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Hi All,

We are facing multiple class loading issue.
The class loader loads the class from some jar and next time its loads the same class from different jar.
We want to do something so that only specific class should get loaded from specific jar file.

Thanks
Swapnil

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There are at least two options:

1) Don't deploy multiple jars with the same class. Doing so means that unless you can specifically order the class resolution you're doomed to randomness.
2) Rename the class(es) inside one of the jars and update your code to use the new packaging (something like jarjar could do this).

#1 is better.
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
I cant Adopt both the solution. Its our business requirement.so I want some handler or something which will load the class according to request.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

As I said, unless you can precisely control classloader behavior, you cannot do what you want to do without (a) a sane deployment strategy, or (b) shading the classes in one of the two jars.

You *might* be able to write your own classloader and use that depending on your execution environment. Doing so is working around the problem rather than fixing it.

I'm pretty sure you don't mean "adobe".
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
If that is an actual business requirement -and I have a hard time believing that it is- then you should support it properly through the software. Either write your own classloader as David suggested, or use a framework like OSGi that gives you fine-grained control over which classes are loaded from which jar file, and which can cope with classes appearing in multiple jar files.
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Thanks Lester and David for reply.
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Class loader works on some delegate principle. It means that if any jar is available in java lib or ext folder , then it will execute. If you are using web application, then you are ovveride this behaviour by putting jars in server lib.

For example

<Glass fish install dir>\domains\domain1\lib. Place that jar files which you want to make sure to excute at this location. Now your application will get jars from application server lib not from your own web-inf or ear lib.

Hope this will solve your issue. (only workaround).

-Sunil
 
GeeCON Prague 2014
 
subject: Multiple class loading issue