Difference between extracting and packaging libraries into a jar file

JavaJarExecutable

Java Problem Overview


I would like to know the difference between extracting and packaging libraries into a jar file from eclipse with the runnable jar file creation.

If my program (runnable jar) uses other classes which require these external libraries(jars), what should I pick?

Java Solutions


Solution 1 - Java

If you want to put jars into your generated jar file, you can use packaging method. For example if you are using an Apache library or some other 3rd party jars, you may want to keep these jars preserved in your generated jar. In this case, use packaging. "Packaging required libraries into a jar file" option puts classes of org.eclipse.jdt.internal.jarinjarloader package into your generated file and this package is just under the root directory of the generated jar file. This option also creates a larger jar file in terms of size due to jar loader classes of Eclipse.

Extracting required libraries will result in putting classes of 3rd party libraries into your jar file by following the package naming convention, e.g. if you open your jar content you can see some classes under org.apache.. packages.

Main class entries are different between the MANIFEST.MF files of these jar files:

Main class entry when you package required libraries:

Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Main class entry when you extract required libraries:

Main-Class: YourMainClass

Solution 2 - Java

For my use, the principal difference is that packaged JAR files are included intact as a distinct item, hence retaining their copyright information and signature data.

If you choose extract, the class files are pulled out of their original context and stored as if you had originated them, hence possibly violating some licence conditions, although the size of the final JAR will be smaller in this case. Eclipse does warn you about licensing in this case, too.

So, if using third-party JAR libraries, it's professional to always package.

Solution 3 - Java

Try it both ways, and open the resulting jar files with a Zip program. Very instructive.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarketView Question on Stackoverflow
Solution 1 - JavaJuvanisView Answer on Stackoverflow
Solution 2 - JavaMikeWView Answer on Stackoverflow
Solution 3 - JavabobanahalfView Answer on Stackoverflow