javascript - How to create a node js function that always wait for new lines from java system.out.println? -


i seek ideas on how can create function wait new lines due delay java app via stdout?

i want display terminal console in manner data presented progressively instead of 1 whole chunk.

here java codes:

public class helloworld {     public static void main(string[] args) {         // prints "hello, world" terminal window.         // system.out.println("hello, worldzzz");         (int i=1; i<11; i++) {             try {                 thread.sleep(500);             }             catch (exception e) {             }              system.out.println("count is: " + i);         }     } } 

here node js codes far:

var http = require('http'),     util = require('util'),     url = require('url'),     exec = require('child_process').exec,     child,     result = '',     poort = 8088;  http.createserver(function(req, res) {     res.writehead(200, {         'content-type': 'text/plain'     });      var pathname = url.parse(req.url).pathname;     if (pathname == '/test') {         res.write('test:');         child = exec('java helloworld', function(error, stdout, stderr) {             console.log = function(d) {                 process.stdout.write(d + stdout + '\n');             };             //console.log('stdout: ' + stdout);             console.log('stderr: ' + stderr);              if (error !== null) {                 console.log('exec error: ' + error);             }         })         res.end();     } else {         res.write('operation not allowed');         res.end();     }  }).listen(poort);     

instead of using child_process.exec() need use child_process.spawn() , listen events.

examples

here simplified example waits java program finish , prints in 1 big chunk:

var exec = require('child_process').exec; var child = exec('java helloworld', function (error, stdout, stderr) {   if (error) {     console.error('error:', error);   } else {     process.stdout.write(stdout);   } }); 

here example of node program processing every line it's printed java program:

var spawn = require('child_process').spawn; var child = spawn('java', ['helloworld']); child.on('error', function (err) {   console.error('error:', err); }); child.stdout.on('data', function (data) {   process.stdout.write(data.tostring()); }); 

keep in mind data buffer not string - if need string have use data.tostring() (here work without .tostring() because process.stdout.write() can take buffers arguments added clarity).

you may need listen close event know when it's finished writing output:

var spawn = require('child_process').spawn; var child = spawn('java', ['helloworld']); child.on('error', function (err) {   console.error('error:', err); }); child.stdout.on('data', function (data) {   process.stdout.write(data.tostring()); }); child.on('close', function (code) {   console.log('child exited code', code); }); 

see the documentation of spawn more details.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -