13

I have added some external libraries to my java project (in netbeans).
Is it possible to put the external jar library to the java archive (and not to put them into a separate (for example) "lib" directory)?

user
  • 5,658
  • 17
  • 52
  • 83

6 Answers6

4

Of course you can. There are a few open source projects that can be downloaded with a "bundle jar" that contains all the dependencies.
You need to extract all the jars and then jar them again to one file.
An example of doing this with Ant can be seen here.

Community
  • 1
  • 1
abyx
  • 61,118
  • 16
  • 86
  • 113
1

The JarJar project allows you to package depend jars into a single jar file for distribution.

The project provides an ant task to do the packaging and as netbeans projects use Ant for their build process, you should be able to integrate it into your build fairly easily.

Robert Christie
  • 18,551
  • 7
  • 39
  • 36
1

Yes, this is possible and the resulting jar is actually called an "uberjar" or "megajar". Basically, to create an "uberjar", you'll need to extract the content of the external library (using jar -x) and to repack it in your own jar (with jar -c). This can be achieved by hand, or with a build tool like Ant (and the optional support of a project like One-JAR) or Maven which has built-in support for this through the maven-assembly-plugin (or the maven-shade-plugin)

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
0

There's an easy way to do it with the built in ant scripts.

There was another SO question pointing to this article: http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/

I added it, but was such a noob I didn't know how to use it. This article had a great visual tutorial. Basically, after modifying your ant script, go to files tab (in explorer on left) => right click on the xml => run tasks => other tasks => package for store (or whatever you named the script)

Raekye
  • 4,795
  • 8
  • 45
  • 72
0

If it is about packing jars into jars this is always done for web apps in war or ear files. If your app is standalone you can unzip the jars and pack them alltogether in one resulting jar using the Ant jar and unjar tasks: http://ant.apache.org/manual/Tasks/unzip.html But I would not recommend to do this.

martin clayton
  • 72,583
  • 29
  • 209
  • 194
stacker
  • 64,199
  • 27
  • 132
  • 206
0

If you are using Maven, you can consider the Maven shade plugin, or the assembly plugin.

gpampara
  • 11,599
  • 3
  • 25
  • 26