File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ant, Maven and Other Build Tools and the fly likes Compile and run a java program thru ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Compile and run a java program thru ant" Watch "Compile and run a java program thru ant" New topic
Author

Compile and run a java program thru ant

srinivasrao marri
Ranch Hand

Joined: Feb 13, 2001
Posts: 61
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

Joined: Jan 30, 2003
Posts: 17
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

Joined: Feb 13, 2001
Posts: 61
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

Joined: Jan 30, 2003
Posts: 17
I'm guessing that the values of 'classpath' and 'main' are not set. In which case

should work.
sai murali
Greenhorn

Joined: Oct 03, 2001
Posts: 13
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.


saimurali
srinivasrao marri
Ranch Hand

Joined: Feb 13, 2001
Posts: 61
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

Joined: Jan 30, 2003
Posts: 17
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

Joined: Feb 13, 2001
Posts: 61
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

Joined: Sep 03, 2001
Posts: 35
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


SCJP2<br /> <br /><a href="http://www.primeguru.com" target="_blank" rel="nofollow">Share Your Personal Experiences</a>
srinivasrao marri
Ranch Hand

Joined: Feb 13, 2001
Posts: 61
send ur build file so that we can exactly come to know the problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile and run a java program thru ant
 
Similar Threads
Problem in schema export - Hibernate
java.lang.VerifyError:
Ant CLASSPATH Question
urgent help on passing parameters thru ant script
Execute my java program with Ant