所以,我一直在开发一个Java应用程序,它应该让人们使用带接口的程序运行他们的Minecraft服务器。运行shell脚本时,所有输出都放在窗口中,然后您可以编写输入。然后,过程读取输入并进行解释。这使您可以使用子命令,例如" start," "停止," "帮助" "运算,"等等。但是,我无法弄清楚如何将输入发送到流程。
这就是控制台的样子:
我尝试了以下内容:
public void createConsoleInput(final InputStream inputStream /* Obtained via process.getInputStream() */)
{
final Thread consoleThread = new Thread()
{
@Override
public void run()
{
final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
consoleInput.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
String cmd = consoleInput.getText();
lastmessage = cmd;
System.out.println("» /" + cmd);
br.read(cmd.toCharArray());
consoleInput.setText("");
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
});
}
};
consoleThread.start();
}
该字段,我标记为"输入"在上面的图片中,当加速时,会执行actionPerformed()方法。但是,似乎冻结了这一行:br.read(cmd.toCharArray());
在外部杀死进程后解冻。它甚至不会执行System.out部分,直到 它被杀死之后。所以,我的问题是:我该怎么做呢?
修改
将其更改为编写器后,它不再挂起。相反,它只是打印控制台消息:
public void createConsoleInput(final BufferedWriter writer)
{
final Thread consoleThread = new Thread()
{
@Override
public void run()
{
consoleInput.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
String cmd = consoleInput.getText();
lastmessage = cmd;
System.out.println("» /" + cmd);
writer.write(cmd);
consoleInput.setText("");
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
});
}
};
consoleThread.start();
}
可以像这样访问作者:
BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
答案 0 :(得分:0)
固定!我只需要冲洗作者。看看:
public void createConsoleInput(final BufferedWriter writer)
{
final Thread consoleThread = new Thread()
{
@Override
public void run()
{
consoleInput.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent action)
{
try
{
String cmd = consoleInput.getText();
lastmessage = cmd;
System.out.println("» /" + cmd);
writer.write(cmd + "\n");
consoleInput.setText("");
writer.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
};
consoleThread.start();
}