You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by e <lo...@menta.net> on 2002/06/11 03:58:33 UTC

How to Redirect the Input of the Java Task

Hello,
Simple, is there any way to redirect the estandar input of a java program 
executed by the Java task?

I've been looking for it, but there's nothing, just a question a year ago 
with no answer :-(
I'm using Ant inside a project, so I've extended some Ant tasks in order to 
work on my way, but I don't know how to change the Java task to do that.

Something like setOutput(output), but for input :-)

I can not use the solution of shell because I would like to use just Java 
everywhere,
<exec executable="sh" dir="">
	<arg value="-c" />
	<arg value="java myJava &lt; input.txt " />
</exec>

I tryied with
	project.setInputHandler(new myFileInputHandler())
but it is never called. The Java program keeps hung waiting I don't know 
what :-)

Any idea will be apreciated :-), or just pointing which class I should look 
for...
Thanks!

* Ermengol *


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Redirection of the standar input of Java Task

Posted by e <lo...@menta.net>.
Few days ago I asked the way to redirect the standard input of the Java Task,
I've found a solution (at the end of the message) but it doesn't accept the 
fork option.
I would like to use the fork option for security reasons, but I don't know 
how to do it. I've been trying diferent things with no luck :-(

Any ideas?
Thanks!

* Ermengol *

----------------------------------------------------------------------------------------------------
import java.io.*;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.*;

/**
* Extension of the org.apache.tools.ant.taskdefs.Java in order to enable
* the redirection of the Standar Input of the Java application loadded.
* It just add another param to the Java task, "input".
* By now, it doesn't accept the fork option, because then I don't know
* how to make the redirection :-/, and it gets hung...
* Any ideas will be welcome
* 14 june 2002.
* @author ebota@uoc.edu
*/
public class InOutJava extends org.apache.tools.ant.taskdefs.Java
{

	InputStream origIn= System.in;
	InputStream stdin= null;

	public InOutJava () {
		super();				
	}

      /** It controls that fork it's not enabled
      */
      public void setFork(boolean b) throws BuildException {
	     if (b) {
		     String msg="InOutJava: Fork not available when redirecting standar 
input";
		     log(msg, Project.MSG_ERR);
		     throw new BuildException(msg);
	     }
	     else super.setFork(b);
      }

      /** The new Standard Input
      */
      public void setInput(File in) {
	     try {
	     stdin= new FileInputStream(in);
	     } catch (IOException e) {	
		     String msg="InOutJava error opening input file";
		     log(msg, Project.MSG_ERR);
		     throw new BuildException(msg,e);
	     }
      }

      public void execute()  {
		try {			
			System.setIn ( stdin );
			super.execute();
			stdin.close();
			System.setIn(origIn);			
		} catch (Exception e) {
			System.out.println("InOutJava Error: "+ e);
		}		
	}	
}
  


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>