command line interface - How to use CLI application from node.js child process? -
i wordering how use application command line interface node.js. here node.js code:
var spawn = require('child_process').spawn; var child = spawn('java', ['helloworld']); child.stdout.pipe(process.stdout); child.stdin.write("tratata\r\n;"); child.stdin.end();
it runs java helloworld cli app.
here java code:
import java.io.console; public class helloworld { public static void main(string[] args) { system.out.println("started"); console console = system.console(); while (true) { string s = console.readline(); system.out.println("your sentence:" + s); } } }
but doesn't work. when child.stdin.write
executs - nothing happening.
disclaimer: know next nothing java.
my guess java.io.console
lot more reading stdin, opening console device (/dev/tty
on unix-like os'es) itself, thereby circumventing stdin/stdout/stderr
devices being passed in node.
if use java.io.bufferedreader
, works better:
import java.io.ioexception; import java.io.inputstreamreader; import java.io.bufferedreader; public class helloworld { public static void main(string[] args) { system.out.println("started"); bufferedreader reader = new bufferedreader(new inputstreamreader(system.in)); while (true) { try { string s = reader.readline(); if (s == null) return; // end of stream reached system.out.println("your sentence:" + s); } catch(ioexception e) { } } } }
Comments
Post a Comment