I have a jar target in ant that makes a jar out of two classes that I have. Then, I want to run this jar in another target that depends on the jar target. Would there be an easy way to do this in ant? I have my compile and jar targets pasted below.
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
</project
To run a Java application, use the java task (see https://ant.apache.org/manual/Tasks/java.html for its documentation). An example from the docs:
<java jar="dist/test.jar" fork="true" failonerror="true" maxmemory="128m">
<arg value="arg1"/>
<arg value="arg2"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
Related
When I'm using external libraries (lucene) and running my java application through eclipse (run application) all works fine with the libs in the classpath.
But when I'm using Ant, i got this error here:
java.lang.ClassNotFoundException: org.apache.lucene.store.Directory
I guess this error shows up, cause of an incorrect classpath. Stange that no compilation error occurs, when ant compiles the code. This is my ant file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build">
<path id="classpath">
<pathelement location="bin"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/analysis/common/lucene-analyzers-common-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/core/lucene-core-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/grouping/lucene-grouping-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/queryparser/lucene-queryparser-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/queries/lucene-queries-6.2.0.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="init" name="build">
<javac debug="true" destdir="bin" includeantruntime="true">
<src path="src"/>
<classpath refid="classpath"/>
</javac>
</target>
<target name="jar" depends="build">
<mkdir dir="GUI_P"/>
<jar destfile="GUI_P/GUI_P.jar" basedir="bin">
<manifest>
<attribute name="Main-Class" value="gui.Gui"/>
</manifest>
</jar>
</target>
<target name="copySamples" depends="jar">
<copy todir="GUI_P/samples">
<fileset dir="src/gui/samples"/>
</copy>
</target>
</project>
Can you please help me here out?
It's not that strange, because ClassNotFoundException arises always when trying a dynamic instantiation (through Class.forName()). So it is possible that the same classpath produces a right compilation, but it is not complete for an execution: It lacks the dynamic dependencies.
In your case, you must add to the execution classpath the lucene-core library (at least).
I have been able to successfully compile and run this project using ant. But when i try to run my test files, it gives me a strange Reference ./lib/junit-4.11.jar not found. I probably am doing something wrong in the refid that i have to mention in the junit task. Please point out the mistake.
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="./src/com/twu/biblioteca/" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Application.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="com.twu.biblioteca.Application"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Application.jar" fork="true"/>
</target>
<target name="junit" depends="compile">
<junit printsummary="yes" haltonfailure="no">
<!-- Project classpath, must include junit.jar -->
<classpath refid="./lib/junit-4.11.jar" />
<!-- test class -->
<classpath location="./test/BooksController" />
<test name="com.twu.biblioteca.BooksControllerTest"
haltonfailure="no" todir="./report">
<formatter type="plain" />
<formatter type="xml" />
</test>
</junit>
</target>
The problem is with your junit classpath elements. The refid attribute allows you to refer to a predefined reference and ant is notifying you that there is no such reference. Additionally, you should have only one classpath element under the junit task (it may work with more than one, but the documentation supports only one).
Remove those classpath elements and replace them with
<classpath>
<pathelement location="lib/junit-4.11.jar"/>
<pathelement location="test/BooksController"/>
</classpath>
and it should work. Here we build up the classpath by adding the junit jar to the path and then the directory containing your tests. You will probably have to add your main classes to this as well, with another pathelement giving their location.
See the ant documentation for more on these path-like structures.
I am trying to run my java program using ant. The compile and jar part work perfectly fine. But when u try to run my created Jar, it shows me
Error: Could not find or load main class com.twu.biblioteca.Application.
Cannot seem to figure out the problem
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Application.jar" basedir="build/classes/com/twu/biblioteca/">
<manifest>
<attribute name="Main-Class" value="com.twu.biblioteca.Application"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Application.jar" fork="true"/>
</target>
The problem is your jar task. From the documentation, basedir corresponds to:
the directory from which to jar the files.
As such, it needs to be
<jar destfile="build/jar/Application.jar" basedir="build/classes">
<!-- ... -->
</jar>
With your configuration, you are making a JAR of only the biblioteca folder, therefore, it doesn't match anymore the package declaration that contains com.twu.biblioteca.
I have following ant script to generate the jar file
<project name="myProject" basedir="." default="jar">
<property name="src" value="Java Source"/>
<property name="output" value="bin"/>
<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="myProject.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="myProject.jar">
<fileset dir="bin"/>
</jar>
</target>
<target name="clean">
<delete dir="${output}"/>
</target>
<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>
When I run ant script i get following error
Reference myProject.classpath not found.
I am not sure how to solve this error. It requires path of .classpath file ?
I also tried with
refid="classpath"
and it didnt work.
Can anyone help please!
Thanks
You need to define first something like because right now MyProject.classpath is not defined:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
assuming that your classpath has what you need.
If it does not, create another entry under classpath element that has references to jars or whatever you need, or you need to custom specify path:
<path id="MyProject.classpath">
<pathelement location="lib/"/>
<pathelement path="${classpath}/"/>
<pathelement path="${additional.path}"/>
</path>
http://ant.apache.org/manual/using.html#path
I am new to ant, but am trying to create an ant script that builds my current proeject with another project as a dependency. I have the ant script building my current project, but am unsure how to add the other project to the classpath. Neither project get put into jar files at the moment.
My current portion of the build.xml file is
<target name="run" depends="compile">
<java classname="com.mypackage.Main">
<classpath>
<pathelement location="../project1/out"/>
<pathelement location="${bin}"/>
</classpath>
</java>
</target>
Thanks for your help!
I was able to do it using
<target name="run" depends="compile">
<java classname="com.mypackage.Main" fork="true">
<classpath>
<dirset dir="${other.dir}">
<include name="out/**"/>
</dirset>
<pathelement location="${bin}" />
</classpath>
</java>
</target>
Where ${other.dir} is the relative path to the root of the other project.