开发者

Ant hangs when running java class that reads from std in

I have a java class that reads from std in. When I run this c开发者_高级运维lass from ant using the java task ANT just hangs and doesn't display my prompt for input nor does it accept input from the command line. It just hangs there. If I run the same class using a shell script it works.

Does anyone know why ANT is doing this? I've tried setting fork (on the java task) to true and false. Neither one worked.


Ant is doing some System.in and System.out intercepting (watch what happens when you do a System.out.println() from inside a custom Task), but you could accomplish what you are after by working with the Input task

http://ant.apache.org/manual/Tasks/input.html

For example, you could have a Task called AccountSettings with the following code

public class AccountSettings extends Task {
  private String guid;
  public AccountSettings() {
    super();
    guid = null;
  }

  public void execute() throws BuildException {
    if(guid != null) {
      //Do something with guid
    } else {
      throw new BuildException("guid not defined!");
    }
  }

  public void setGuid(String value) {
    guid = value;
  }
}

And then have the "guid" value set via the input task with the following bit of xml

<taskdef name="settings" classname="AccountSettings"/>
<input message="Enter an account guid" addproperty="ACCOUNT.GUID"/>
<settings guid="${ACCOUNT.GUID}"/>


Can you share you code, because I've just tried out this example, using this simple build.xml and it worked for me:

<project default="run">
<target name="run">
<java classpath="." classname="ReadString"/>
</target>
</project>

The output was ugly, but it worked.


If (as @Jason Sperske says) Ant intercepts (replaces) the System.* streams, then another alternative is to modify your Java class to use the java.io.Console class to read and write to the console. However, note that this circumvents any redirection of the standard streams that (for example) you might do when running Ant from the command shell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜