如何在 nodejs 和 java 之间建立通信?

How to build a communication between nodejs and java?

我有Foo.java个文件

package mypack;
class Foo {
  public int add (int a, int b) {
    return a+b;
  } 
}

和一个Bar.java

package mypack;
class Bar {
  public static void main (String args[]) {
    Foo instance = new Foo();
    System.out.println(Foo.add(2, 3));
  }
}

我有一个 server.js 文件,我在其中使用 shelljs 读取输出并在终端 shell.

中打印出来
var shelljs = require('shelljs');

shelljs.exec('java -cp bin mypack.Bar', function (code, output) {
   console.log(output);
});

当我运行node server.js 我得到输出

5
5

第一个由 java 打印语句打印,而第二个由 nodejs 控制台语句打印。 我的问题是如何修改 java 输出,使其只被节点读取而不打印在 shell 中? 所以,我只得到节点控制台语句的输出。

看起来有一个 silent 选项告诉它不要将程序输出打印到控制台:

shelljs.exec('java -cp bin mypack.Bar', {silent: true}, function (code, output) {
   console.log(output);
});

看看documentation