tags:

views:

187

answers:

5

How do you include .jar files in a Java library so that it can be imported directly in a program?

+1  A: 

hmm. using ant the folloeing may help

<target name="CreateExecutableJarFileWithExternalLibrary">
 <jar destfile="MyApplication.jar">
  <zipfileset dir="classes" prefix="" />
  <zipfileset src="external.jar" />
  <manifest>
   <attribute name="Main-Class" value="mypackage.MyMainClass" />
  </manifest>
 </jar>
</target>

But you shall provide more information. In general I don't think that it's good style to provide a jar in a jar...

tuergeist
A: 

I think your question needs to be clarified.

But, either add them to your CLASSPATH environment variable or use -classpath on java.exe

csl
A: 

I think you want to include a classpath entry in the MANIFEST.MF. I can't believe this question hasn't been already answered on SO. Here are the instructions. Best way to to this ist using the ant snippet from tuergeist, because fiddling with the classpath entry is a bit cumbersome.

UPDATE: This works also inside a EAR file. Most container do allow referencing JAR files inside the ear itself. The container has to use a special class loader for this.

dz
But the entries in a MANIFEST ClassPath must refer to jar files outside the jarfile, not bundled jar files.
jmanning2k
+1  A: 

Sounds like you want to bundle your project, including dependencies, into a single jar file.

Try one of the following:

jmanning2k
A: 

To clarify, you can't enclose jars within jars. If you're build a .jar library, then you effectively have 2 options:

  1. build your .jar of your code, and specify the additional .jars to go alongside. They will have to be referenced on the classpath with your jar
  2. or, explode the additional .jars, and rejar with your code into one huge .jar. That has the pro of being simple to manage, and the downside that you should be careful exploding and recombining jars in case there are duplicate classes and you get the wrong one (or omit non .class resources etc.)
Brian Agnew