Results 1 to 7 of 7
  1. #1
    prsindagi is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Run ant scripts using ProcessBuilder

    This is what I have done so far:


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;


    public class BuildClass {


    public BuildClass() {
    }
    public static void main (String args[]) throws Exception{


    String s = null;
    ProcessBuilder pb = new ProcessBuilder();


    Map<String,String> env = pb.environment();
    String path=null;
    if(env.containsKey("Path")){
    path=env.get("Path");

    }

    path=path+";C:\\ANT 1.7.0\\bin\\;";
    env.put("JAVA_HOME","C:\\Program Files\\Java\\jdk1.5.0_10");
    env.put("Path",path);

    pb.redirectErrorStream(true);
    pb.directory(new File("C:/xyz/pqr/uvw/"));


    pb.command("ant","jar");
    Process p = pb.start();
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((s = stdInput.readLine()) != null) {

    System.out.println(s);
    }

    }
    }



    ----------------
    When i run this i get:
    Exception in thread "main" java.io.IOException: CreateProcess: ant jar error=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :451)
    at BuildClass.main(BuildClass.java:51)
    Java Result: 1


    But if I change pb.command("ant","jar"); to pb.command("notepad");
    Its working fine.

    Could anyone help

  2. #2
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    Can you check if ant.bat is in PATH variable?
    you may also want to try ant.bat instead of only ant.

    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal| Snacks

  3. #3
    prsindagi is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    I have already put the ant in the path :

    path=path+";C:\\ANT 1.7.0\\bin\\;";

  4. #4
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    try C:\\ANT 1.7.0\\bin\\ant instead of just ant

    and see if it gives same error?
    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal| Snacks

  5. #5
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    also try without "jar" option ... let me know if that works....

    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal| Snacks

  6. #6
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    I am able to run this using this line


    pb.command("ant.bat", "jar");

    its related to the name of the file then....

    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal| Snacks

  7. #7
    prsindagi is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    If you see the code I have set the path:

    Map<String,String> env = pb.environment();
    String path=null;
    if(env.containsKey("Path")){
    path=env.get("Path");

    }

    path=path+";C:\\ANT 1.7.0\\bin\\;";
    env.put("JAVA_HOME","C:\\Program Files\\Java\\jdk1.5.0_10");
    env.put("Path",path);


    ----------

    pb.command("C:\\ANT 1.7.0\\bin\\ant.bat","jar"); --- Only this works

    pb.command("C:\\ANT 1.7.0\\bin\\ant","jar"); --- Even this does not work

    pb.command("ant","jar"); --- this won't work
    pb.command("ant.bat","jar"); --- this won't work


    Need to know why?You have any idea

Similar Threads

  1. bug (?) in ProcessBuilder.redirectErrorStream(true);
    By gadasie in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2008, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •