Reading input from keyboard causes ant to hang(?)
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Oliver_Hautz
Posted On:   Monday, June 16, 2003 05:15 PM

Hi, I tried to execute a Java program that reads input from the keyboard with a BufferedReader. This normally works just fine, but when I use ant to run the program, the process hangs right when the readLine() method is used on the BufferedReader object. the code fragment: BufferedReader q = new BufferedReader(new InputStreamReader(System.in)); String quit = q.readLine(); I already set fork to true in the java task, whithout any effect. Does anybody know how to avoid this problem? Thanks alot.    More>>

Hi,


I tried to execute a Java program that reads input from the keyboard with a BufferedReader. This normally works just fine, but when I use ant to run the program, the process hangs right when the readLine() method is used on the BufferedReader object.


the code fragment:


			
BufferedReader q = new BufferedReader(new InputStreamReader(System.in));

String quit = q.readLine();



I already set fork to true in the java task, whithout any effect.


Does anybody know how to avoid this problem?

Thanks alot.

   <<Less

Re: Reading input from keyboard causes ant to hang(?)

Posted By:   Roseanne_Zhang  
Posted On:   Wednesday, June 18, 2003 11:19 AM

I missed the import header, add them on the top of the code.

package setup;

import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;

Re: Reading input from keyboard causes ant to hang(?)

Posted By:   Roseanne_Zhang  
Posted On:   Wednesday, June 18, 2003 11:00 AM

Use this class instead of your BufferedReader will work. This is a working one in our installation process. The original author is credited in the code. I've made some improvement.

/**
* Provides a BufferedReader with a readLine method that
* blocks for only a specified number of seconds. If no
* input is read in that time, a specified default
* string is returned. Otherwise, the input read is returned.
* Thanks to Stefan Reich
* for suggesting this implementation.
* @author: Anthony J. Young-Garner
* @author: Roseanne Zhang made improvement.
*/

public class TimedBufferedReader extends BufferedReader
{
private int timeout = 60; // 1 minute
private String defaultStr = "";

/**
* TimedBufferedReader constructor.
* @param in Reader
*/
TimedBufferedReader(Reader in)
{
super(in);
}

/**
* TimedBufferedReader constructor.
* @param in Reader
* @param sz int Size of the input buffer.
*/
TimedBufferedReader(Reader in, int sz)
{
super(in, sz);
}

/**
* Sets number of seconds to block for input.
* @param seconds int
*/
public void setTimeout(int timeout)
{
this.timeout=timeout;
}

/**
* Sets defaultStr to use if no input is read.
* @param str String
*/
public void setDefaultStr(String str)
{
defaultStr = str;
}

/**
* We use ms internally
* @return String
*/
public String readLine() throws IOException
{
int waitms = timeout*1000;
int ms = 0;
while (!this.ready())
{
try
{
Thread.currentThread().sleep(10);
ms += 10;
}
catch (InterruptedException e)
{
break;
}
if (ms >= waitms)
{
return defaultStr;
}
}
return super.readLine();
}
}


Erik, can you put this in FAQ, many people need it.

Thanks!

Roseanne

Re: Reading input from keyboard causes ant to hang(?)

Posted By:   Anonymous  
Posted On:   Tuesday, June 17, 2003 03:35 AM

With Ant until 1.5.3.1 you canĀ“t. The actual CVS version has the capability to redirect in/output from/to files (see Mail from Connor). I am not sure whether the actual version is capable of redirecting the console inputstream to tasks. But on Mail Ermengol suggested a workaround.
About | Sitemap | Contact