Java 启动 Node.js 程序立即关闭

Posted

技术标签:

【中文标题】Java 启动 Node.js 程序立即关闭【英文标题】:Java start Node.js program immediately closes 【发布时间】:2017-09-04 03:31:37 【问题描述】:

我正在尝试制作一个具有 Java FXML 接口以及 Node.js 程序的 Discord 机器人,以制作音频播放机器人。

当我尝试在 Java 中打开 Node.js 的进程时,我注意到它立即关闭,因为当我尝试编写时,我得到了 IOException pipes are being closed。我不确定为什么会发生这种情况,因为当我在命令行中以完全相同的方式运行 node 时,它​​保持打开状态。有谁知道为什么我的进程会立即关闭?

Node.js 通信类:

public class NodeCommunicationHandler 

private static final Logger LOG = Logger.getLogger(NodeCommunicationHandler.class.getName());

private Process nodeProcess;
private ExecutorService threadPool;
private ProcessHandlerTask callbackHandler;
private StringProperty callback;
private OutputStream writeStream;

/**
 *
 */
public NodeCommunicationHandler() 
    try 
        // Activate Node.js sub-process
        this.nodeProcess = new ProcessBuilder("cmd", "/c", "start", "node", "\"" + Reference.NODE_PROGRAM_LOCATION + "\"").start();

        // Initialize the stdout writing stream
        this.writeStream = this.nodeProcess.getOutputStream();

        // Initialize the stdin reader on the process
        this.callbackHandler = new ProcessHandlerTask(this.nodeProcess);

        // Bind the callback data from the process to the callback property
        this.callback = new SimpleStringProperty();
        this.callback.bind(this.callbackHandler.messageProperty());

        // Run the thread of the process callback handler
        this.threadPool = Executors.newFixedThreadPool(2);
        this.threadPool.submit(this.callbackHandler);
     catch (IOException ex) 
        NodeCommunicationHandler.LOG.log(Level.SEVERE, null, ex);
    


/**
 * Retrieve the internal process callback.
 *
 * @return The callback.
 */
public StringProperty getCallback() 
    return this.callback;


/**
 * Writes a string command to the Node.js process.
 *
 * @param command The command to write.
 */
public void write(String command) 
    try 
        this.writeStream.write(command.getBytes());
        this.writeStream.flush();
     catch (IOException ex) 
        NodeCommunicationHandler.LOG.log(Level.SEVERE, null, ex);
    


/**
 * Stop the Node.js process
 */
public void stop() 
    try 
        // Write the command STOP towards the node process to promptly exit the discord API.
        this.write("STOP");

        // Close the writing stream since STOP is the last command we have to write.
        this.writeStream.close();

        // Wait for the Node.js acknowledgement of the STOP issue.
        NodeProcessExitHandler exitCond = new NodeProcessExitHandler(this.callback);
        this.threadPool.submit(exitCond).get();

        // Unbind the callback listener.
        this.callback.unbind();

        // Stop the callback handler and let the thread finish.
        this.callbackHandler.stop();

        // Keep the process alive until the callback handler has been disconnected.
        this.threadPool.awaitTermination(10000, TimeUnit.MILLISECONDS);

        // Kill the subprocess since we know
        this.nodeProcess.destroy();
     catch (IOException | InterruptedException | ExecutionException ex) 
        NodeCommunicationHandler.LOG.log(Level.SEVERE, null, ex);
    


还有我的 javascript 程序:

var stdin = process.openStdin();

stdin.addListener("data", function(d) 
    console.log("You entered: " + d.toString());
);

javascript 真的很基础,但它应该能够提供我需要的东西。

编辑:

问题描述中的小错误。 当进程在 Java 中运行时,它会打开一个控制台窗口,Node 程序实际上会继续运行,但 Outputstream 只是断开连接并抛出 IOException。

【问题讨论】:

【参考方案1】:

问题在我启动应用程序的第二天自动解决,进程能够连接和发送数据。我不知道是什么导致它一开始就不起作用。

【讨论】:

以上是关于Java 启动 Node.js 程序立即关闭的主要内容,如果未能解决你的问题,请参考以下文章

如何立即关闭Node.js http(s)服务器?

在 Node.js 中发送电子邮件? [关闭]

Node.js的嵌入式文档数据库[关闭]

Nodejs调用webservice [关闭]

Netflix使用Node.js+React.js让网站启动更快

node --- 服务一直启动