ant cannot identify my jar files at all - java

I have my jar files in Assignment2\lib folder and my build file is in Assignment2. The name of the jar file is Assignment1.jar The following is how I tried to compile my Assignment2 from build file through ant.
<project name="Assignment1" default="run" basedir=".">
<property name="classes" value="classes" />
<path id="project.class.path">
<pathelement location="src"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<path id="lib.jars">
<fileset dir="lib" includes="**/*.jar" />
</path>
......
<target name="compile" description="compaling java files with Assignment2">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" debug="on" failonerror="true">
<classpath refid="project.class.path"/>
<classpath refid="lib.jars"/>
</javac>
</target>
<Project/>
I am getting compile errors. I'm using windows. Is that the problem? Is there any way to compile?

Well,Everything looks good to me but, you can try the following to validate the same,
Check if the lib folder has all the necessary library jars required to compile you application
You can try assigning the relative path of the lib folder to a property and refer it in the classpath eg: Property lib value=".\Lib"
Try the following before you call the compile target to see the list of jars being included in the classpath
<pathconvert property="libjars" refid="lib.jars"/>
<echo>libjars is ${classpathProp}</echo>

hey guys thanx for the answers actually i found probelm. i was trying to use a jar file created by netbeans files. Apparently netbeans jar files can be used only by netbeans. at the end i made a new jar file of Assingment1 and used it. now everything is fine. Thanx again for the answers.

Related

How to include external class files during build in ant

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

Compiling with ANT using external libs

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>

java.lang.NoClassDefFoundError + ant - running a jar

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.

Ant include external .jar

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}" />

Package Problem with Ant

I'm having a problem getting the javac used by Ant to find and use certain packages. When I invoke javac directly from the command line the packages are found and used.
The .jar files are located in my home directory under lib/java. This is my classpath:
/home/bliskovs/lib/java/*:/home/bliskovs/vendor/cytoscape-v2.7.0/cytoscape.jar
This is the relevant section in my build.xml:
<target name="compile">
<javac srcdir="." debug="true"/>
<javac srcdir="tools/" debug="true"/>
<javac srcdir="core/" debug="true"/>
</target>
How can I get Ant to recognize these packages?
Check out this.
<property name="build.classes.dir" location="build/classes"/>
<path id="compile.classpath">
<fileset dir="lib"/>
<pathelement location="/home/bliskovs/vendor/cytoscape-v2.7.0"/>
</path>
<target name="compile" description="Compile src dir">
<javac destdir="${build.classes.dir}" debug="true" includeantruntime="true">
<src location="src"/>
<classpath refid="compile.classpath"/>
</javac>
</target>
Define a classpath for the javac task. Relying on the CLASSPATH environment variable is a bad practice. It's even more true for the build process of a project, which should work without having to setup a whole lot of environment variables. If you start developing three or four projects at once, you'll understand why using a single CLASSPATH env variable is a bad idea.
See http://ant.apache.org/manual/Tasks/javac.html to know how to define a classpath inside the build.xml and use it in the javac task.

Categories