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

时间:2017-04-08 20:57:46

标签: javascript java node.js discord

我试图制作一个具有Java FXML界面的Discord机器人以及一个Node.js程序来制作一个音频播放机器人。

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

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 个答案:

答案 0 :(得分:0)

第二天,我启动了应用程序,该问题自动解决了,并且该过程能够连接并发送数据。我不知道是什么原因导致它不能起作用。