I am creating jar via eclipse, but not runnable jar, it is just a library. After importing that i'm creating new project and adding this jar file. But when run application it throws exception about not finding libraries which are inside in my jar. But these libraries are included in created jar. Where is my mistake ? How can I do that ?
-
Have you added that jar to build path properly? – Nullpointer Apr 24 '14 at 14:21
-
1"But these libraries are included in created jar." What does that really mean? Did you create a fat-jar? – Gimby Apr 24 '14 at 14:23
-
@Gimby when I unzipped jar, other libraries were there. But I didn't use fat-jar plugin of eclipse. – GirginSoft Apr 24 '14 at 14:26
-
You are saying the other jars are INSIDE your own jar library? – Gimby Apr 24 '14 at 14:26
-
If it is about including jar within a jar, see http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar – Artur Malinowski Apr 24 '14 at 14:27
-
@Nullpointer I think yes, because there is no compilation error. It throws exception on run time. java.lang.NoClassDefFoundError: org/apache/http/message/BasicNameValuePair this class used by my library – GirginSoft Apr 24 '14 at 14:27
-
I would like to see/know the resource that told you that you can do that. Because you can't. Not out of the box anyway, you need specialized tooling to be able to work with something like that. – Gimby Apr 24 '14 at 14:30
2 Answers
You cannot, by default, include dependency JAR files inside your JAR file, you need a plugin, as specified in Classpath including JAR within a JAR (suggested by Artur Malinowski). If, however, you wish to create a JAR which simply works with your other dependencies, you can easily do this via Eclipse by right clicking on the project, going into properties, Java Build Path, Libraries and then you can add JARs, etc. by using the buttons along the right side of the menu (as of Kepler).
I hope this helps.
P.S. You'll have to cart round the dependency JAR files wherever you put the main JAR, however, this is the easiest and most straightforward approach to getting it working in the first place, and then refine later, using build plugins.

- 1
- 1

- 237
- 2
- 6
I found the solution. I write some ant script for creating jar and it was solved my problem.
<target name="dist" depends="build-jar">
<property name="store.jar.name" value="MYJARNAME"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging project into a single JAR at ${store.jar}"/>
<mkdir dir="dist"/>
<delete dir="${store.dir}"/>
<mkdir dir="${store.dir}"/>
<mkdir dir="dist/lib"/>
<copy todir="dist/lib/" overwrite="false" granularity="9223372036854">
<fileset dir="lib/"/>
</copy>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<zip destfile="${store.jar}">
<zipfileset src="${store.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
</target>

- 375
- 3
- 16