0

I have a build.xml to generate Jar file for my Application .

I am trying to call the build.xml directly as shown below

import java.io.IOException;

public class RunBuild {

    public static void main(String args[]) throws IOException {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("/home/sai/Jan5WS/AntTest/build.xml");
    }

}

Its not throwing any Exception , but its not generating the jar file .

I am using ubuntu 12 and ant 8.1 verions currently

Could anybody please tell me what could be wrong ??

Thanks

Edited Part

I have tried running it this way

public static void main(String args[]) throws IOException {

File buildFile = new File("build.xml", "/home/sai/Jan5WS/AntTest");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());

}

But its throwing

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.tools.ant.Project.initProperties(Project.java:313)
    at org.apache.tools.ant.Project.init(Project.java:300)
    at RunBuild.main(RunBuild.java:15)
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • How are you running the code? Your class path may not have all libraries. Is there a ClassNotFoundException in the log ? – Jayan Feb 05 '13 at 05:26

5 Answers5

1

Assuming normal ant, you cannot execute the build.xml.

You need to run ant and pass build.xml as argument.

(It is possible to run an ant project using its libraries : See this question Is it possible to call Ant or NSIS scripts from Java code? )

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143
1

You have two options:

  1. Execute ant in new JVM using System.execute("ant", "-f", "/home/nitesh/Projects/ANT_DEMO/RPM_ANT/SIS/build.xml")
  2. Execute build in same JVM using ProjectHelper, code sample:

        File buildFile = new File("/home/sai/Jan5WS/AntTest", "build.xml");
        Project p = new Project();
        p.setUserProperty("ant.file", buildFile.getAbsolutePath());
        p.init();
        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, buildFile);
        p.executeTarget(p.getDefaultTarget());

In this case ant.jar and ant-launcher.jar must be on classpath.

Cyril Sochor
  • 692
  • 6
  • 9
0

build.xml is not executable file. It is just a plain xml file. You need to execute "ant" and provide it the path of this build.xml file via "-f" flag from your code.

Prateek
  • 523
  • 2
  • 13
0

Try it out its working for me ....

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class CreateJar {

        public static void main(String[] args) {

                Process process;
                try {
                        process = new ProcessBuilder("ant","-f" ,"/home/nitesh/Projects/ANT_DEMO/RPM_ANT/SIS/build.xml").start();
                        process.waitFor();
                        InputStream is = process.getInputStream();
                    InputStreamReader isr = new InputStreamReader(is);
                    BufferedReader br = new BufferedReader(isr);
                    String line;
                    while ((line = br.readLine()) != null) {
                      System.out.println(line);
                    }

                } catch (Exception e) {
                        e.printStackTrace();
                }



        }

}

here is build.xml

<project name="rpmextract" default="jar" basedir=".">
<property name="build" location="build"/>
<target name="jar">
<jar destfile="app.jar" basedir="${build}/classes"/> 
</target>
</project>
Nitesh Singh Rajput
  • 607
  • 1
  • 7
  • 24
0

I had the same issue, I solved it with following steps:

1.- download ant-launcher.jar for your ant version

2.- add it to your libraries

enter image description here

Miguel Benitez
  • 2,322
  • 10
  • 22