1

The Java documentation includes a note about adding nested jars into the classpath.

To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes.

There are many tools that do this, such as the ones listed here and here.

  1. Do these tools work simply by extracting classes from the nested jars and adding the extraction path to the classpath? Or does it take more than simply unzipping the archives?
  2. Is there a technical reason for the limitation that the manifest.mf classpath can point to the local file system, but not inside its own archive?
Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83

1 Answers1

0

Another option if you are using Maven is the Shade Mojo. It will explode all of the JARs, allowing their contents to be packaged with your code. It is also capable of other magic, like moving dependencies into custom packages to avoid conflicts and merging files found in META-INF.

One of the primary problems with this is that often JARs will expose artifacts at exactly the same location. This can be problematic for (usually JDK) systems that allow extension via ServiceLoader. These files need to be intelligently merged / concatenated.

Another API that could be effected in subtle, possible bug causing ways, is ClassLoader.getResources(String).

If you are using a SecrutiyManager things can get even more complicated with security domains.

tl;dr It's a limitation largely driven by the ClassLoading API

Steve Skrla
  • 1,620
  • 5
  • 16
  • 24
  • I'm not convinced that the potential for conflicts is a sufficient reason for the manifest.mf not to support nested jars. Conflicts arise regardless of any dependency management strategy. For example, see: http://stackoverflow.com/questions/30768120/resolving-transitive-dependency-conflicts-in-java. – jaco0646 Jun 18 '15 at 20:04
  • Well you have questions of semantics, what does a nested jar imply? Are all of the nested jars visible to the root class loader? Does each nested JAR get it's own classloader? Are nested jars private to the jar above them? It is entirely possible for you to implement a classloader that does all of this, it's just not part of the standard. – Steve Skrla Jun 18 '15 at 20:16
  • IMO, dependency conflicts is another issue entirely and present whether you flatten or not. If anything, it is more likely to cause you problems if you are *not* flattening as you'll get CNF exceptions if two jars define the same class. – Steve Skrla Jun 18 '15 at 20:19