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
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"/>
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
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>
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}" />
Related
I have a package(with some outdated Java files) which i need to build using ant tool. I have some updated classes for which source is not available. So, I need to add the updated class to the jar during build and skip the outdated Java files from the build. Please let me know how to achieve this.
For example - I dont have the updated source java file for com.org.auth.ABC.Java, but i have the updated class file which i got from the another source ie com.org.auth.ABC.class. During build i need to point to the updated class(com.org.auth.ABC.class) for this class alone and create a new jar.
Please find how i am currently pointing to the class files below.
<target name="xxx.jar" depends="xjc,compile">
<jar destfile="${dist.dir}/xxx.jar">
<fileset dir="${classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-source.dir}" includes="**/bgm.ser,**/jaxb.properties"/>
</jar>
</target>
If you want to leave some package from compilation you can use excludes attribute of fileset ant tag.
for example:
<fileset dir="src/">
<exclude name="**/dir_name_to_exclude/**" />
</fileset>
In order to include the specified class in compilation you can put the containing folder in your class-path using ant.
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="path to your class file's base folder"/>
</path>
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="project.class.path">
... put source files here..
</javac>
And if you want to include that class-file in your jar then add it to fileset using include tag:
<fileset dir="classfiles">
<include name="your class file name"/>
</fileset>
Hope this helps
I'm new with ANT and I'm trying to compile part of my Eclipse project. I have many classes but i need to pack only a part of it (please don't ask why). My problem is that one of these classes references to an external library placed in the <project_root>/libs folder and I did not find out how to link it. I found examples on the web but I was not able to arrange it.
<path id="classpath">
<fileset dir="libs" includes="**/*.jar"/>
</path>
<target name="compile">
<mkdir dir="client/classes"/>
<javac srcdir="src" destdir="client/classes" sourcepath="classpath">
//include needed java files
</javac>
</target>
I'm using the annotation #Remote of EJB. It's in the javax.ejb package. I get the error:
package javax.ejb does not exist
[javac] import javax.Ejb.Remote;
If I understand your project structure correctly, the issue with your ant file is that you reference the classpath as beeing the sourcepath.
sourcepath / sourcepathref point to locations where source can be found. I suppose what you want is classpathref="classpath".
<javac srcdir="src" destdir="client/classes" classpathref="classpath">
//include needed java files
</javac>
try this one
<classpath>
<fileset dir="libs">
<include name="**/*.jar"/>
</fileset>
</classpath>
Tagging- Selenium as well just in case someone faced similar issue while creating selenium tests using Ant.
I have seen lot of questions/answers on this topic, tried all the options suggested on various forums but still my issue is not getting resolved. Basically i compile code(includes the test scripts), create JAR and run the same JAR. For some reason it does not seem to identify the libraries during run time. Same code(With tests) works fine when main() method is run from Eclipse. Here is the build.xml,
<project default="run">
<target name="clean">
<delete dir="build" />
</target>
<target name="init-classpath">
<path id="lib.classpath">
<fileset dir="./lib/">
<include name="**.jar" />
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="lib.classpath" />
<flattenmapper />
</pathconvert>
</target>
<target name="jar" depends="clean, init-classpath">
<javac classpathref="lib.classpath" destdir="./compiled" failonerror="true" srcdir="./src" />
<mkdir dir="build/jar" />
<jar destfile="build/jar/BANC.jar" basedir="compiled">
<manifest>
<attribute name="Main-Class" value="com.portico.driver.TestDriver" />
<attribute name="Class-Path" value="${mf.classpath}" />
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="build/jar/BANC.jar" fork="true">
</java>
</target>
</project>
Error:-Exception in thread "main" java.lang.NoClassDefFoundError: jxl/Workbook
Manifest content
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_41-b02 (Sun Microsystems Inc.)
Main-Class: com.portico.driver.TestDriver
Class-Path: activation.jar commons-lang-2.4.jar jna-3.4.0.jar jxl.jar
logging-selenium-1.2.jar mail.jar ojdbc14.jar poi-3.0.2-FINAL.jar rep
ortng-1.1.1.jar saxon-8.7.jar selenium-grid-demo-1.0.7.jar selenium-g
rid-demo-standalone-1.0.7.jar selenium-grid-hub-1.0.7.jar selenium-gr
id-hub-standalone-1.0.7.jar selenium-grid-remote-control-1.0.7.jar se
lenium-grid-remote-control-standalone-1.0.7.jar selenium-grid-tools-1
.0.7.jar selenium-grid-tools-standalone-1.0.7.jar selenium-server-1.0
.3-standalone.jar selenium-server-standalone-2.33.0.jar sikuli-script
.jar testng-6.8.jar velocity-1.7.jar
The first thing to check is, whether the problem is connected with the manifest or something else. If you can run your application with java -cp <allthejarfiles> <main-class> the problem is connected with the manifest. Keep in mind that the jar files specified in the manifest are relative to the jar file’s location. Trying to run the application with the -verbose:class option gives hint about which jar are really loaded.
Your manifest assumes the jars in the current working directory. So it would require dir attribute set to the folder where the jar exists.
Java task supports providing classpath in the arguments. Try giving the classpath in arguments.
<target name="run" depends="jar">
<java jar="build/jar/BANC.jar" fork="true" dir="build/jar" >
<classpath>
<pathelement path="${lib.classpath}"/>
</classpath>
</java>
</target>
I have faced the same issue in my project. I suggest that you should create a separate directory specially for the jar files, put all of your jars in that directory, and then point the lib address to that directory.
for example in your case say I have created a directory D:/jar_collection, where I have put all my jars physically.
<property name="lib.dir" value="D:/jar_collection"/>
<target name="setClassPath">
<path id="classpath_jars">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>
and it works fine. Please try it once.
I have the following file structure:
ServerCode <- src , libs, bin
I am trying to compile all the code in src. Src has a couple of .java files at the top level and sub-directories. libs contains all the .jar files which are required to build my project.
I wrote the following build.xml but when I try to compile it, the compiler throws errors cannot find symbol errors for the libraries I am including.
<project default="compile">
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" classpath="libs/*.jar">
</target>
</project>
Define class path to include all jars like this
<target name="compile" depends="" description="compile the java source files">
<javac srcdir="." destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${test_lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
I don't think you can use a pattern in the classpath attribute. I could be wrong about this. You should run ant in verbose mode (the -v option) to see how it's using your classpath attribute. I suspect it's passing it to javac as a literal string.
Here's what I do:
<javac target="${javac.target}" source="${javac.source}" destdir="${validator.output.dir}" debug="on"
nowarn="off"
memorymaximumsize="128m" fork="true">
<classpath refid="validator.module.production.classpath"/>
<src>
<dirset dir="Validator">
<include name="src"/>
</dirset>
</src>
</javac>
...
<path id="validator.module.production.classpath">
<fileset dir="Validator/lib/-validator" includes="**/*.jar"/>
</path>
Some of this code is generated by my IDE so it's a little verbose, but you get the idea.
Try this as mentioned at http://ant.apache.org/manual/using.html instead of giving classPath attribute alongwith javac
<classpath>
<pathelement location="libs/*.jar"/>
</classpath>
There are other ways also which you can glance thru the link mentioned above
I am creating a jar file of a javaagent that I have written. However, that agent depends on another java library.
So, I would like to know, how do I include a whole package that is being used by my program into the jar file so that it can be used when the jar gets executed?
Unzip the jar you want to include and put it the final jar. Here are the ant commands that will do this for you.
<target name="package">
<!-- Staging is simply a temporary directory for exploding the jar files. You
can call the directory whatever you want. -->
<property name="staging.dir" location="staging"/>
<mkdir dir="${staging.dir}"/>
<unjar dest="${staging.dir}">
<fileset dir="lib" >
<include name="*"/>
</fileset>
</unjar>
<mkdir dir="classes"/>
<javac debug="on" srcdir="src" destdir="classes" target="1.5" includes="**/*">
<classpath refid="build.class.path"/>
</javac>
<jar destfile="Product.jar"
basedir="classes"
excludes="**/Test.class">
<fileset dir="${staging.dir}"/>
</jar>
<!-- Delete the temporary staging directory. -->
<delete dir="${staging.dir}" />
</target>