Java:在程序关闭之前,没有来自进程对象的输入
我试图从由Java脚本启动的.exe进程的控制台获取输入。控制台窗口中不显示任何内容,并且在进程终止之前,程序不会读取任何内容Java:在程序关闭之前,没有来自进程对象的输入,java,input,process,Java,Input,Process,我试图从由Java脚本启动的.exe进程的控制台获取输入。控制台窗口中不显示任何内容,并且在进程终止之前,程序不会读取任何内容 blServ = new ProcessBuilder(blPath + "Blockland.exe", "ptlaaxobimwroe", "-dedicated", "-port " + port, "-profilepath " + blPath.substring(0, blPath.length() - 1)).start(); System.out.pri
blServ = new ProcessBuilder(blPath + "Blockland.exe", "ptlaaxobimwroe", "-dedicated", "-port " + port, "-profilepath " + blPath.substring(0, blPath.length() - 1)).start();
System.out.println("Attempting to start server...\n" + blPath);
consoleIn = new BufferedReader(new InputStreamReader(blServ.getInputStream()));
“blServ”是一个进程对象。是的,该计划正在成功启动
public void blStreamConsole() //called once every 500 milliseconds
{
String lineStr = "";
String line = "";
int lines = 0;
try
{
if (consoleIn != null)
{
while ((line = consoleIn.readLine()) != null)
{
//if (!line.equals("%"));
//{
lineStr += line + wordSym;
lines++;
//}
}
}
}
catch (IOException e)
{
netOut.println("notify" + wordSym + "ERROR: An I/O exception occured when trying to get data from the remote console. Some lines may not be displayed.");
}
if (!lineStr.equals("") && !(lineStr == null))
netOut.println("streamconsole" + wordSym + lines + wordSym + lineStr);
}
基本上,这个方法会查看consoleIn对象中是否有更多的输入等待,如果有,它会将它拥有的每一行附加到另一个字符串,并将另一个字符串发送到客户端。不幸的是,当Blockland.exe关闭时,它会在一大块中发送。很抱歉缩进问题。Stackoverflow编辑器重新排列了所有代码。在我看来,这里有两种可能性:
阻塞,等待输入(并且不会像预期的那样返回readLine
)。您可以通过使用null
而不是来修复它BufferedReader
- 在写入所有输入之前,输出流不会刷新。尝试将
放在那里: 另外请注意,如果flush
为lineStr
,您将得到一个null
,与代码当前的状态相同(您需要交换条件),但它甚至不能为NullPointerException
null
if (!lineStr.isEmpty()) { netOut.println("streamconsole" + wordSym + lines + wordSym + lineStr); netOut.flush(); }
lineStr
,直到程序退出(当console.readLine()
为null
时)。然后打印包含整个控制台的整个lineStr
如果要连续打印输出,需要立即打印:
while ((line = consoleIn.readLine()) != null){
netOut.println(line);
}
您可以在一个单独的线程中运行它,它将继续将控制台输出到输出流,直到程序退出
while ((line = consoleIn.readLine()) != null){
netOut.println(line);
}