I found this useful when passing information between java calls. 

The outputproperty is used in the same way as Exec, but for Java. Currently 
only implemented for calls when the fork attribute is set to true. 


Dennis Adams 

 

 
--- org\apache\tools\ant\taskdefs\Java.java.orig        Thu Oct 11 23:58:28 2001
+++ org\apache\tools\ant\taskdefs\Java.java     Thu May  2 14:44:19 2002
@@ -72,14 +72,16 @@
  * @author <a href="mailto:[EMAIL PROTECTED]">Stefan Bodewig</a>
  */
 public class Java extends Task {
-
+    private static String lSep = System.getProperty("line.separator");
     private CommandlineJava cmdl = new CommandlineJava();
     private boolean fork = false;
     private File dir = null;
     private File out;
     private PrintStream outStream = null;
     private boolean failOnError = false;
-    
+    private String outputprop; 
+    private ByteArrayOutputStream baos; 
+
     /**
      * Do the execution.
      */
@@ -251,6 +253,14 @@
     }
 
     /**
+     * Property name whose value should be set to the output of
+     * the process
+     */
+    public void setOutputproperty(String outputprop) {
+      this.outputprop = outputprop;
+    }
+
+    /**
      * -mx or -Xmx depending on VM version
      */
     public void setMaxmemory(String max){
@@ -313,13 +323,16 @@
         FileOutputStream fos = null;
         try {
             Execute exe = null;
-            if (out == null) {
-                exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
-                                                       Project.MSG_WARN), 
-                                  null);
+            if (out != null) {
+              fos = new FileOutputStream(out);
+              exe = new Execute(new PumpStreamHandler(fos), null);
+            } else if (outputprop != null) { 
+              baos = new ByteArrayOutputStream();
+              exe = new Execute(new PumpStreamHandler(baos), null);
             } else {
-                fos = new FileOutputStream(out);
-                exe = new Execute(new PumpStreamHandler(fos), null);
+               exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
+                                                       Project.MSG_WARN), 
+                                  null);  
             }
             
             exe.setAntRun(project);
@@ -335,7 +348,27 @@
             
             exe.setCommandline(command);
             try {
-                return exe.execute();
+                int rVal = exe.execute();
+
+                if (baos != null) 
+                {
+                  BufferedReader in = new BufferedReader(new 
StringReader(baos.toString()));
+                  String line = null;
+                  StringBuffer val = new StringBuffer();
+                  while ((line = in.readLine()) != null) 
+                  {
+                    if (val.length() != 0) 
+                    {
+                      val.append(lSep);
+                    }
+                      val.append(line);
+                  } 
+
+
+                  project.setProperty(outputprop,val.toString());
+                }
+              
+                return rVal;
             } catch (IOException e) {
                 throw new BuildException(e, location);
             }

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

Reply via email to