Running java classes with ant - java

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.

Related

Junit tests wont run using ant

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.

run target in ant that depends on jar

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>

using less utility in ant

I am trying to write a target in ant to display files using the unix utility less but for some reason it keeps failing to build because of this target. I have pasted my whole ant file below including this target. It keeps saying this:
Attribute name "PlayTour.java" associated with an element type "less" must be followed by the ' = ' character.
What would be causing this? I am rather new to ant so any help would be greatly appreciated.
Ant file:
<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>
<target name="view">
<less "PlayTour.java KnightsTour.java"/>
</target>
</project>
less isn't a valid ant task. As you've pointed out, it's a unix command.
You're probably looking for the exec task.
Try something like:
<target name="view">
<exec executable="less">
<arg value="PlayTour.java" />
<arg value="KnightsTour.java" />
</exec>
</target>

how to add source code to jar file using ant build.xml

I want to add all my source code to jar file using ant while creating jar file from source code.
I want to have two files 1- myProject.jar 2-myproject_source.jar
What should i use and where should i put it?
<project name="myProject" >
<target name="clean">
<delete dir="./build"/>
</target>
<target name="compile">
<mkdir dir="./build/classes"/>
<javac srcdir="./src" destdir="./build/classes"/>
</target>
<target name="jar">
<mkdir dir="./build/jar"/>
<jar destfile="./build/jar/DependencyFinder.jar" basedir="./build/classes">
<manifest>
<attribute name="DependencyFinder" value="main"/>
</manifest>
</jar>
</target>
</project>
Add this just before your <manifest> line:
<fileset dir="./src" includes="**/*.java"/>
You can add mulitple <fileset> statements if you want.

Ant not able to locate external jar files. Showing java.lang.NoClassDefFound Error exception

I am creating a jar file using following target -
<target name="jar" depends="compile">
<jar destfile="${jar.dir}/TargetClass.jar"
basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
</target>
and I'm trying to execute the jar file using following target -
<target name="runjar">
<java jar="${jar.dir}/TargetClass.jar" fork="true"/>
</target>
I've used apache poi jar in my TragetClass which is located under D:/Jar directory. While executing it's not able to locate the poi jars and showing java.lang.NoClassDefFoundError exception.
You need to use a classpath. Something like:
<property name="poi.jars" value="path to poi jars"/>
<path id="run.classpath">
<pathelement location="${poi.jars}"/>
</path>
<target name="runjar">
<java
jar="${jar.dir}/TargetClass.jar"
fork="true"
<classpath refid="run.classpath"/>
/>
</target>
See Ant Java Task for more information.

Categories