I need to programmatically (in Java) run JavaScript with Node.js and read the error. There are two approach:
node -e <javascript>
node temp.js
How are they different?
It seems that the first approach is more efficient, but when I run something like
process.on('uncaughtException', function(err) {
console.error(typeof(err));
});
function x_5(x_6, x_7) {
var x_8 = Infinity;
}
throw x_5;
It prints
[eval]:8
throw x_5;
^
function
But all I wanted was just the last line. The second approach only gave me the last line. So I am trying to understand how the two approaches are different, and how I can get rid of the unnecessary error message.
as you see node -e script
will dump exception to stderr, so you can print the result to stdout.
console.log instead of console.error
process.on('uncaughtException', function(err) {
console.log(typeof(err));
});
on java side, take result from stdout, ignore stderr
String[] command = {"node", "-e", js};
Process p = Runtime.getRuntime().exec(command);
InputStream in = p.getInputStream();
for (int i = 0; i < in.available(); i++) {
System.out.println("" + in.read());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.