Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile and run a java program thru ant

 
srinivasrao marri
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i am able to compile my java program thru ant, but its failing to run it. For compiling
<target name="build">
<javac srcdir="." />
<java classname="${main}" classpath="${classpath}"/>
</target>
Can any one tell the same for running the java class file.
Thanks
Srinivas
 
Kieran Bee
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the error messages? What are the values of 'main' and 'classpath'. Is the xml you gave supposed to compile and run the code, or just compile it? From the ant manual, running a java class goes something like:
 
srinivasrao marri
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
my java code is
class test{
public static void main(String args[]) {
System.out.println("welcome to ant");
}
}
and i made it to compile thru ant and the code is for build.xml file i written is
<?xml version="1.0"?>
<!-- build file for lesson 1 -->
<project name="tutorial" default="build" basedir=".">
<target name="build">
<javac srcdir="." />
<java classname="${main}" classpath="${classpath}"/>
</target>
</project>
for the same program if i want to make it run what kind of line of code i need to add for build.xml?
Thanks
srinivas
 
Kieran Bee
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that the values of 'classpath' and 'main' are not set. In which case

should work.
 
sai murali
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change your build.xml as follows:
<?xml version="1.0"?>
<project name="test" default="main" basedir=".">
<property name="src" value="." />
<property name="build" value="." />
<path id="project-classpath">
<pathelement location="."/>
<pathelement location="${build}"/>
</path>
<target name="init">
<tstamp/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" />
</target>

<target name="run">
<java classname="test" fork="yes" >
<classpath refid="project-classpath" />
</java>
</target>

<target name="main" depends="compile,run" />
</project>
and try again.
 
srinivasrao marri
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i made my class public, with this my build.xml is running fine, the code i mentioned above is the one i used. i hope the reason to make my class public is , the ant itself if a java class file and its try to use another class file, if i declare it as public , then it can have access to it. i welcome any further discussion on this.
thanks
srinivas
 
Kieran Bee
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't run a method on a non-public class in the way it is invoked through Ant, that includes the Main method
 
srinivasrao marri
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u mean to say this way:
class HelloWorld{
void method1(){
System.out.println("i am in method 1");
}
public static void main(String args[]){
HelloWorld hw=new HelloWorld();
hw.method1();
}
}
o/p:
i am in method 1
this i did without ant....
could u pls elaborate u'r sentence...
Thanks
srinivas
[ February 25, 2003: Message edited by: srinivasrao marri ]
 
Sri Kor
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have similar problem. When I tried to run my java application with Ant build file, My application opens and closes immidiately.
I think, ant is killing the process immediately.Any comments
Thanks
 
srinivasrao marri
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
send ur build file so that we can exactly come to know the problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic