Difference between extracting and packaging libraries into a jar file

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?

36983 次浏览

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

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.

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

These are mainly two ways to export as a Runnable jar in eclipse.

1). Package required libraries into jar

  • This will add actual jar files of the libraries into your jar. This is the cleanest since it separates application class files with library JARs.
  • The downside is this makes runnable jar perform very slow.

2).将所需的库提取到生成的JAR中

  • This way will extract only the actual class files from the libraries that your application uses and includes in the runnable jar file. As a result your Jar file will include your application class files as well as class files of all the libraries used by your application.

  • This method makes runnable jar perform just like it is run in your eclipse IDE.