Ant Freezes with no warning on exception - java

I have been experiencing a weird problem that I can't find a solution to. When I am running a project through ant and there's an uncaught exception, ant simply freezes with absolutely no error. Here is my build file. This is a multi threaded environment, also
<fileset id="masterlibs" dir="${lib.dir}">
<patternset>
<include name="*.jar"/>
</patternset>
</fileset>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement location="${build.dir}"/>
<fileset refid="masterlibs"/>
</classpath>
</javac>
</target>
<target name="run" depends="compile">
<java classname="stockData.AlertHandler" fork="true">
<classpath>
<pathelement location="${build.dir}"/>
<fileset refid="masterlibs"/>
</classpath>
<arg line="true"/>
<arg line="true"/>
<arg line="true"/>
<arg line="true"/>
<arg line="false"/>
<arg line="27_4"/>
<arg line="false"/>
</java>
</target>

Ctrl+Break will show you a dump of threads and indicate what each is doing (plus any deadlocks). I'm guessing it's your spawned AlertHandler process and it may be easier to debug this outside the Ant environment.
If Ctrl+Break doesn't work (from the article)
On UNIX platforms you can send a
signal to a program by using the kill
command. This is the quit signal,
which is handled by the JVM. For
example, on Solaris you can use the
command kill -QUIT process_id, where
process_id is the process number of
your Java program.

Ant can sometimes freeze when it runs a java class that asks for interactive input. Is it possible that your AlertData class asks for input?

Related

ant build file terminating tag error

I am trying to run an ant build file for a java jar. For some reason, whenever I try to run ant run, it comes back and says, "Element of type target must be terminated by the matching </target> tag" This seems strange because I see that I do have a terminating tag for run. Would there be something else that could be causing this? Any help would be greatly appreciated!
<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= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar"/>
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
<target name="view">
<exec executable="less">
<arg value="PlayTour.java" />
<arg value="KnightsTour.java" />
</exec>
</target>
<target name= "doc">
<mkdir dir="build/docs"/>
<javadoc sourcefiles="KnightsTour.java, PlayTour.java" destdir="build/docs"/>
</target>
</project>
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar"/>
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
You are closing the Java tag in the block above twice, first using /> in-line then writing </java>, which causes your error. This is what it looks like fixed:
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar" >
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
Whenever you suspect an XML validation error, you should use a free online XML validation utility to at least confirm that the structure of your file is correct. This is what I used to quickly locate your error: http://www.xmlvalidation.com/index.php?id=1&L=0

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>

Running One Server and Two Clients using ANT

I am a newbie into ANT world. My Application is based on Client-Server architecture which is using "RMI" for communication.
I need to write an ANT build script which automatically compiles the whole code, starts the server and run two clients connected to that server.
Here's structure of my current build.xml file.(I am not sure what's wrong)
<?xml version="1.0" encoding="UTF-8"?>
<project default="runClientOne" name="MyFirstAntProject">
<target name="compile">
<javac srcdir="./src" destdir="classfiles" />
</target>
<target name="runServer" depends="compile" >
<java classname="com.jain.RMIServer">
<classpath path="classfiles" />
</java>
</target>
<target name="runClientOne" depends="runServer">
<java classname="com.jain.RMIClient" fork="true" taskname="A" >
<classpath path="classfiles" />
<arg value="localhost"/>
<arg value="Sumit"/>
</java>
<java classname="com.jain.RMIClient" fork="true" taskname="B">
<classpath path="classfiles" />
<arg value="localhost"/>
<arg value="Sushil"/>
</java>
</target>
</project>
Thanks Guys,
I have figured out that I will have to use the <parallel>My CODE </parallel> .
I stumbled upon this question when I was trying to accomplish a similar thing, here is how I achieved a target that starts running the server and client. (My answer expands on Sushil's and this SO answer)
Here is the case: I have a server, and when I start up I want 3 clients to connect to the server right away, all in a separate process.
The trick is to nest parallel and sequential in the build.xml targets individually.
The sleep call can be changed to wait on the process too.
<target name="start-all" depends="build"
description="Starts 1 Server and 2 clients ">
<parallel>
<sequential>
<java classname="Server" fork="true">
<classpath refid="master-classpath"/>
<arg line="${port}"/>
</java>
</sequential>
<sequential>
<sleep milliseconds="500"/>
<antcall target="start-client">
<param name="server.address" value="localhost" />
<param name="server.port" value="${port}"/>
</antcall>
</sequential>
</parallel>
</target>
This would start the server, and then the clients, alternatively, using <daemon> to start the server and then immediately launching the clients right after.

Regarding ant: how i can give few aruments to main function?

Here is my "run" in my ant build file:
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
I want to run it like this:
ant run -Darg0=First.txt -Darg1=Second.txt -Darg2=Third.txt -Darg3=Fourth.txt -Darg4=Fifth.txt
What changes should i make in my "run"?
Many thanks for help!
Here's the ant task to run any program (including, but not limited to Java programs):
<target name="run">
<exec executable="name-of-executable-file">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</exec>
</target>
Here's the task to run a Java program from a .jar file:
<target name="run-java">
<java executable="path for jar">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</java>
</target>
You can invoke either from the command line like this:
ant -Darg0=Hello -Darg1=World run
Updated task
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
<arg value="${arg0}" />
<arg value="${arg1}" />
</java>
</target>
Check out the <java> task documentation
Use nested <arg> and <jvmarg> elements to specify arguments for the
Java class and the forked VM respectively.
and the <arg> subtask

Groovy unable to resolve java class

i got a problem by executing groovy from an ant file.
In Eclipse with a launcher, everything works fine but wehn i run the ant file i got the following output:
Main.groovy: 71: unable to resolve class InitializeDatabase
[groovyc] # line 71, column 40. [groovyc] java.lang.Object
javaClassInstance = new InitializeDatabase()
[groovyc]
[groovyc] 1 error
InitializeDatabase is a java class in the same package..
public class InitializeDatabase {
public void test() {
System.out.println("Hello Groovy");
}
}
I guess the problem is located at the ant file:
<project name="tp" basedir="." default="dbsetup">
<target name="dbsetup">
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar" />
</classpath>
</taskdef>
<delete dir="bin" />
<mkdir dir="bin" />
<groovyc srcdir="src" destdir="bin" />
<java classname="groovy.ui.GroovyMain" dir="../.." fork="true" failonerror="true">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar"/>
<pathelement location="bin"/>
</classpath>
<arg line="build/scripts/src/build/Main.groovy" />
</java>
</target>
</project>
Can someone help me please?
You need to include the javac task inside your groovyc one. Change:
<groovyc srcdir="src" destdir="bin" />
to
<groovyc srcdir="src" destdir="build">
<javac/>
</groovyc>
And it should work fine. As it says here:
Joint compilation means that the Groovy compilation will parse the
Groovy source files, create stubs for all of them, invoke the Java
compiler to compile the stubs along with Java sources, and then
continue compilation in the normal Groovy compiler way. This allows
mixing of Java and Groovy files without constraint.
...
The right way of working is, of course, to use a nested tag and all
the attributes and further nested tags as required.
Here is the final file which works great.
Thanks to tim_yates!
<target name="dbsetup">
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar" />
</classpath>
</taskdef>
<delete dir="bin" />
<mkdir dir="bin" />
<groovyc srcdir="src" destdir="bin">
<javac source="1.6" target="1.6" debug="on" />
</groovyc>
<java classname="groovy.ui.GroovyMain" dir="../.." fork="true" failonerror="true">
<classpath>
<fileset dir="../files/lib/default" includes="*.jar"/>
<pathelement location="bin"/>
</classpath>
<arg line="build/scripts/src/build/access/AccessDbSetup.groovy" />
</java>
</target>

Categories