Can I call class files that are within a jar library located in another jar file?

Chathuranga Chandrasekara picture Chathuranga Chandrasekara · Aug 11, 2009 · Viewed 9.6k times · Source

I am trying to access the class files packaged as a library but unfortunately the jar libraries should be packaged in another jar file.

As an example say I have a.jar that contains some class libraries. I can call (import) the classes in the jar file from my external java application. Now I need to put this a.jar inside another jar say b.jar and need to access (import) the classes in a.jar from outside of the b.jar.

This is not the thing I want to do :)

Answer

Jon Skeet picture Jon Skeet · Aug 11, 2009

You would have to write a classloader which was able to deal with "jars within jars." This sounds like a pretty bad idea to me.

Don't merge the jar files - just have them separately on disk. I realise you may need to persuade management of the necessity of having more files, but it really is going to make everything a lot simpler. Writing a classloader is non-trivial...