Ant script to generate Jar - Reference not found error - java

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

Related

Ant / External libraries

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).

Need to make a ant build file for eclipse project

I have a simple game implemented in eclipse. It consists of about 8 classes.
It is for my school assignment.
In the turn in specification, there is written:
"Send me all source codes, documentation and ant build file, which allows the project to be compiled and generate javadoc documentation".
I really do not understand how ant works. I googled some tutorials, but I cannot understand them either. I tried to generate build.xml file in eclipse, but the teacher said that this doesnt work either.
Could someone give me some simple steps or give me link to some really basic tutorial? Thanks for help.
This is the eclipse generated ant (export project as antbuildfile):
And it is kind of weird, because the class BasicPaint I deleted a long time ago.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="Snakes_and_Adders">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<path id="Snakes_and_Adders.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="Snakes_and_Adders.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target name="BasicPaint">
<java classname="snakes_and_adders.BasicPaint" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="Game">
<java classname="snakes_and_adders.Game" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="NewGame">
<java classname="snakes_and_adders.NewGame" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="PaintingExample">
<java classname="snakes_and_adders.PaintingExample" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
Ant is used to perform tasks that are useful to build applications. You have tasks like <javac> <jar> etc.. To compile your classes and put them in a jar file.
I don't see why the build.xml generated file wouldn't work.. But you can take it as an example to understand how ant works. You can also adapt that build.xml file to make it work anywhere.
This tutorial looks well explained at first sight: http://www.javaworld.com/article/2076208/java-app-dev/automate-your-build-process-using-java-and-ant.html
I find that ant can be pretty complex easily, it'll take you time to understand it well but it's really doable.

Run multiple JUnit tests with Ant

probably this is a very basic question but I am new to java, so please bear with me.
I am trying to run java task in ant and I want to know if there is any way to get all the name of the classes inside a folder in ant?
This is the build.xml file
<project default='run-test' basedir=".">
<property name="src" value="src"/>
<property name="test" value="test"/>
<property name="build" value="build"/>
<property name="build-classes" value="build/classes"/>
<property name="build-test-classes" value="build/test/classes"/>
<property name="junit4" value="/usr/share/java/junit4.jar"/>
<target name="init">
<mkdir dir="${build-classes}"/>
<mkdir dir="${build-test-classes}"/>
</target>
<target name="compile" depends="init">
<javac includeantruntime="false"
srcdir="${src}"
destdir="${build-classes}"/>
</target>
<target name="compile-test" depends="compile">
<javac includeantruntime="false"
classpath="${junit4}:${build-classes}"
srcdir="${test}"
destdir="${build-test-classes}"/>
</target>
<target name="run-test" depends="compile-test">
<java classpath="${junit4}:${build-classes}:${build-test-classes}"
classname="org.junit.runner.JUnitCore"
args="myclass.MyClassTest"/> <---This is my problem
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
</project>
I have to manually mention the name of the class in the arguments.
Is there any way where I can only mention the folder and all the class names will be taken up as arguments for the javatask automatically?
Ant ships with a task for running JUnit tests.
In the run-test target in your example, replace the <java> task with the following:
<junit>
<formatter type="plain" usefile="false"/>
<classpath>
<pathelement location="${junit4}"/>
<pathelement location="${build-classes}"/>
<pathelement location="${build-test-classes}"/>
</classpath>
<batchtest>
<fileset dir="${build-test-classes}">
<include name="**/*.class"/>
</fileset>
</batchtest>
</junit>

Could not find .Main, error with Ant when I try to Run it

This is my first time using ant and one of my first times I program in java so I'm a little lost, I'm trying to compile and run a program with ant, it seems that it compiles right, but I have problems make it run, it says that it Couldn't find Tarea2.Main.
So this is the structure of my project file:
Tarea 2/
src/tarea/pkg2
build.xml
3rd party .jar
Inside src/tarea/pkg2 I have all my .java, and the .java that has the main method is called Tarea2.java.
And this is my build.xml:
<project name="Tarea2" default="run" basedir=".">
<property name="src" location="src"/>
<property name="build" location="build"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<path id="classpath">
<fileset dir=".">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath refid="classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<java classname="Tarea2.Main" classpath="${build}">
<classpath refid="classpath"/>
</java>
</target>
</project>
So I write "ant" in cmd from windows and everything is ok, except in the "run" target, in which I got stuff like:
[java] Could not find Tarea2.Main. Make sure you have it in your classpath
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute
...
Somebody can help me pls?.

import com.sun.xml.internal.fastinfoset.util.StringArray cannot be resolved at javac

I am trying to write ant script for my project but at javac i am facing this issue:
80: package com.sun.xml.internal.fastinfoset.util does not exist
[javac] import com.sun.xml.internal.fastinfoset.util.StringArray;
[javac] ^
JAVA_HOME is set to jdk1.6
ANT_HOME is set to apache-ant-1.8.4
build file is
<?xml version="1.0" ?>
<project name="project" default="war">
<property name="location" location="D:\Project"></property>
<property name="project-location" location="${location}\project"></property>
<path id="Web App Libraries.libraryclasspath">
<fileset dir="${project-location}/web/WEB-INF/lib"/>
</path>
<path id="EAR Libraries.libraryclasspath"/>
<path id="compile.classpath">
<pathelement location="${project-location}/web/WEB-INF/classes"/>
<path refid="Web App Libraries.libraryclasspath"/>
<path refid="EAR Libraries.libraryclasspath"/>
</path>
<path id="Server Library [JBoss v4.2] (unbound).libraryclasspath"/>
<target name="init" >
<mkdir dir="D:/JBOSSHOME/build/classes"/>
<mkdir dir="D:/JBOSSHOME/dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="D:/JBOSSHOME/build/classes" debug="true" srcdir="${project-location}/src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="D:/JBOSSHOME/project.war" webxml="${project-location}/web/WEB-INF/web.xml">
<fileset dir="${project-location}/web"/>
<lib dir="${project-location}/web/WEB-INF/lib"/>
<classes dir="D:/JBOSSHOME/build/classes"/>
</war>
</target>
any help or pointer would be much appreciated ty!
com.sun.internal packages are NOT recommended to use , because
they are not guaranteed to be available across environments
support to those classes are also not guaranteed

Categories