Ant include external .jar
Asked Answered
O

4

7

I want to include external jar to my java project. I'm using ant. External .jar is in folder lib. My build.xml looks something like that:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <path id="classpath">
        <fileset dir="lib" includes="**/*.jar"/>
    </path>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build"/>
        <javac srcdir="src" destdir="build" classpathref="classpath" />
    </target>

    <target name="jar">
        <mkdir dir="trash"/>
        <jar destfile="trash/test.jar" basedir="build">
            <zipgroupfileset dir="lib" includes="**/*.jar"/>
            <manifest>
                <attribute name="Main-Class" value="com.Test"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="trash/test.jar" fork="true"/>
    </target>
</project>

But it doesn't work. When I want to import something from the external .jar, there is an error after command ant compile: package com.something does not exist.. What should I edit to get it working?

Exact error:

  Compiling 23 source files to xy/build
  xy/src/com/Test.java:5: package com.thoughtworks.xstream does not exist
  import com.thoughtworks.xstream.*;
  ^
  1 error
Overfeed answered 15/4, 2012 at 18:31 Comment(3)
Can you edit your question to provide us with the exact error?Fireside
I have added the exact error.Overfeed
you can't include external libraries easily in a normal jar. You need special tasks like one-jar to accomplish that.Justen
S
4

You should try without the includes attribute:

<fileset dir="lib" />

And in the jar part you include the classes like this:

<zipgroupfileset includes="*.jar" dir="lib"/>
Sash answered 15/4, 2012 at 18:37 Comment(0)
J
1

You can't put external libraries into a jar and expect the classloader to use those jars. Unfortunately this is not supported.

There are ant tasks like one jar that help you, to create a jar file, that contains everything you need.

This bit is from the background information of one jar:

Unfortunately this is does not work. The Java Launcher$AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end. This approach will only work if you install (i.e. scatter) the supporting Jar files into the directory where the jarname.jar file is installed.

Another approach is to unpack all dependent Jar files and repack them inside the jarname.jar file. This approach tends to be fragile and slow, and can suffer from duplicate resource issues.

Other Alternative:

  • jarjar: Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution
Justen answered 16/4, 2012 at 6:16 Comment(2)
Not quite accurate, some applications such as Hadoop, for example, support the convention of putting jars in the lib directory of a jar and the application handles getting them on the classpath.Periodicity
@DavidParks but thats because Hadoop uses an own classloaders. So this is application specific and not part of the official jar definiton.Justen
F
0

I also use ant to include a number of dependency JARs in my JAR. My compile task looks like this. Perhaps something similar will work for you.

<target name="compile" depends="init">
   <javac srcdir="${src}" destdir="${build}" includeantruntime="false">
    <classpath>
      <pathelement path="${classpath}" />
      <fileset dir="${deps}">
         <include name="**/*.jar"/>
      </fileset>
    </classpath>
  </javac>
  <copy todir="${build}">
    <fileset dir="${src}" excludes="**/*.java"/>
  </copy>
</target>
Fireside answered 16/4, 2012 at 4:1 Comment(0)
S
0

sometimes u can use jar contents directly, just unzip

        <unzip src="/Developer-Java/mysql-connector-java/mysql-connector-java-5.1.22-bin.jar" dest="${build_dir}" />
Stefanistefania answered 9/1, 2013 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.