Ruby Forum JRuby > 'gets' not reading from provided reader? (JSR223+Jruby)

Posted by John Pritchard-williams (monojohnny)
on 19.08.2008 11:46
The following program almost works: I'm trying to read from a text file,
rather than stdin, but 'gets' seems to insist I type at 'stdin'...

Is this likely a problem in JRuby, The JRuby Engine or something wrong
with the 'javax.script' stuff? Anybody seen this issue (or is this
correct behaviour?)

Cheers

John


package test;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import javax.script.*;

public class TestClass {

public static void main(String[] args) throws Exception {

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine rubyEngine = factory.getEngineByName("jruby");
ScriptContext context = rubyEngine.getContext();

FileWriter out=new FileWriter("C:\\RUBY.OUT");
InputStreamReader in=new InputStreamReader(new
FileInputStream("C:\\IN.TXT"));
context.setWriter(out);
context.setReader(in);

rubyEngine.eval("a=gets;puts a");
out.flush();
out.close();
in.close();
}
}
Posted by John Pritchard-williams (monojohnny)
on 19.08.2008 12:36
Further information:

Even doing this:

//
FileInputStream fis=new  FileInputStream("in.txt");
System.setIn ( fis );
//

Results in 'gets' trying to get input from the existing standard in it 
seems....
I'm running this from a Netbeans IDE...I wonder if that is highjacking 
the in/out streams .....will check running standalone....

Posted by John Pritchard-williams (monojohnny)
on 19.08.2008 12:40
> I'm running this from a Netbeans IDE...I wonder if that is highjacking 
> the in/out streams .....will check running standalone....

Nope...same thing...but I can kinda force it to work (this is no good to 
me in general as it happens) with:

java -jar <myjar> < in.txt


....