A good way to do it is to re-use the <java> task internally, and set the
fork mode on it.  Here's an example that is actually a straight
cut-and-paste from our "Writing Ant tasks" chapter:

    public void execute() throws BuildException {
        Java javaTask = null;

        javaTask = (Java) project.createTask("java");
        javaTask.setTaskName("search");

        javaTask.setClassname("org.example.antbook.tasks.Searcher");

        javaTask.setClasspath(classpath);

        javaTask.createArg().setFile(indexDir);
        javaTask.createArg().setValue(query);

        javaTask.setFork(true);
        if (javaTask.executeJava() != 0) {
            throw new BuildException("error");
        }
    }

I have setters on my task that take the indexDir (java.io.File) and query
(String), as well as allowing nested classpath and classpath/classpathref
attributes.

    Erik


----- Original Message -----
From: "Scott Ellsworth" <[EMAIL PROTECTED]>
To: "Ant List" <[EMAIL PROTECTED]>
Sent: Tuesday, May 28, 2002 10:57 PM
Subject: Hi, all. How do I protect myself from System.exit?


> Hi, all.
>
> I have a task that I have written that calls the main method of a java
> class after doing some parameter munging.  This main method does a
> System.exit.  I cannot easily rewrite that part of the code, so I need
> to work around it.
>
> I presume the "right way" is to just fork.  I looked at the docs, and it
> indicates that Java and ExecuteJava will handle this, but I kind of
> foundered looking at the taskdef javadocs for Java and ExecuteJava.
>
> Assuming I have a Task already written with a stack of instance
> variables, getters, setters, and a working execute method that ends with
> a call to the main class of the thing I am wrapping.
>
> Can anyone point me to how I need to modify this to use the appropriate
> fork code?  What follows is my best attempt, but the call appears to not
> have the same classpath as the task that spawned it.
>
> public class AntIntercalateTask extends Task {
>      boolean _theParam=false
>      public void setTheParam(boolean theParam)
>      {
>          _theParam=setTheParam;
>      }
>      public AntIntercalateTask()
>      {
>      }
>      execute()
>      {
>            // worked, save for System.exit
> //String[] args={"param1", "value1"};
> //com.Foo.main(args);
>
>           // cannot find classpath
>
>          CommandlineJava cmdline = new CommandlineJava();
>          cmdline.createArgument().setValue("param1");
>          cmdline.createArgument().setValue("value1");
>          prepareArguments(cmdline);
>          cmdline.setClassname("com.Foo");
>
>          Execute exe=new Execute();
>          exe.setCommandline(cmdline.getCommandline());
>          try{
>              exe.execute();
>          } catch (Exception e){
>              System.out.println(e);
>          }
>      }
> }
>
> So, how can I transplant the classpath from the taskdef?
>
> Scott
>
>
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to