从 java 内部调用 node.js 脚本

2022-01-11 00:00:00 console node.js java

如何在 java 中调用 node.js 并将 console.log 值保存在 String 变量中?

How can I call a node.js inside java and save the console.log values in a String variable?

推荐答案

Java 应用程序可以与正在运行的 Node.JS 应用程序进行通信.例如,您可以让 Node.JS 应用程序在可用端口上运行,而 Java 应用程序可以通过 tcp 套接字与其通信.

It is possible for a Java application to communicate with a running Node.JS application. For instance, you can have a Node.JS app running on an available port and the Java app can communicate with it via tcp sockets.

http://nodejs.org/api/net.html

或者您可以创建一个 http 服务器并公开一个您的 Java 应用可以使用的 rest 服务.

Or you can create an http server and expose a rest service which your Java app can consume.

http://nodejs.org/api/http.html

或者如 md_5 所说,您可以使用 Runtime.exec,然后在结果进程上调用 getInputStream.

Or as md_5 says, you can use Runtime.exec and then call getInputStream on the resulting process.

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

node.js 和 Java 之间的通信方式与其他可以完成的跨应用程序通信没有什么不同.

The ways you can communicate between node.js and Java are no different from other cross application communication that can be done.

还可以使用 node-java 之类的东西从 Node.JS 应用程序调用 Java 代码.

It is also possible to invoke Java code from your Node.JS application using something like node-java.

https://github.com/nearinfinity/node-java

相关文章