Search code examples
javajarant

How Can I use this command line in Ant


I need to update my file jar using ant by using this line command : jar -uf sample.jar [path in jar file] target-file

I tried this :

<target name="UpdateJar" depends="Compile">
    <exec executable="java path">
        <arg value="-uf"/>
        <arg value="$Sample.jar"/>
        <arg value="${Path}"/>
        <arg value="Test.class"/>
    </exec>
</target>

But It's not working no such file or directory

The error is the following: (path is : X:\Jar\test ): [exec] X:\Jar\test no such file or directory [exec] Result: 1


Solution

  • Probably the "exec" task has an extra argument, I guess you expect to include Test.class from directory "${Path}", I would write instead

    <target name="UpdateJar" depends="Compile">
      <exec executable="java">
        <arg value="-uf"/>
        <arg value="sample.jar"/>
        <arg value="${Path}/Test.class"/>
      </exec>
    </target>
    

    Anyway, for efficiency, knowing a jar file is just a Zip file, I recommend to just update the existing jar file (already containing META/MANIFEST.MF and some classes) with Apache Ant Zip task

    <zip destfile="sample.jar" update="true">
      <fileset dir="${Path}" includes="Test.class"/>
    </zip>
    

    But I guess you also expect to preserve package structure in jar file, which requires your "java" exec to run with proper current working directory. My alternative with zip is then more obvious

    <zip destfile="sample.jar" update="true">
      <fileset dir="${Path}" includes="your/package/Test.class"/>
    </zip>