Put external library to the JAR? [closed]
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this questionI 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)?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.
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.
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)
If you are using Maven, you can consider the Maven shade plugin, or the assembly plugin.
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.
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)
精彩评论