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

问题描述:

我正在尝试制作一个带有Java FXML接口以及用于制作音频播放机器人的Node.js程序的Discord机器人。Java启动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。

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