77

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Arian
  • 3,183
  • 5
  • 30
  • 58
  • possible duplicate of [What is the difference between runnable jar library handling options?](http://stackoverflow.com/questions/8302894/what-is-the-difference-between-runnable-jar-library-handling-options) – The Guy with The Hat Dec 12 '14 at 01:38

4 Answers4

55

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
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • 5
    whats the advantage or the use then of packaing? – Arian Mar 08 '12 at 13:06
  • 2
    by packaging, you can load jar files into your generated jar via "JarRsrcLoader" class. if you are using 3rd party jars and want to combine your application code with these jars, you can use eclipse's packaging utility like this. see my edited answer. – Juvanis Mar 08 '12 at 13:25
  • 12
    Beware of performance issues. I used to package my library jars inside my application jar using jarinjarloader until yesterday. Then, after updating JRE to version 8 update 31 (on windows), executing my application became tremendously slower (up to 10x). The problem was solved when i chose to extract the content of library jars into my jar. – Giuseppe Feb 25 '15 at 11:58
  • Surely @Giuseppe that performance hit will only occur as the class is loaded; once the application is running, it should make no difference .... – MikeW Nov 08 '18 at 11:30
  • 1
    @MikeW That is what you would think, but my application was still about an order of magnitude slower the whole time running with "package". Choosing the "extract" option makes the performance much quicker. – dminear Sep 05 '19 at 19:19
  • @dminear - that's interesting, I think there are tools you can use to identify bottlenecks in speed - that would certainly be of technical interest ! Perhaps it's cached differently .... but I would have thought experimenting with JVM settings might help identify the reason – MikeW Sep 11 '19 at 09:13
5

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.

MikeW
  • 5,504
  • 1
  • 34
  • 29
  • Does convert the project to Maven and then generating the jar will extract or package? – Fadi Nov 11 '22 at 12:11
0

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

bobanahalf
  • 829
  • 1
  • 11
  • 23
0

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). Extract required libraries into generated 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.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51