So, I've been working on a Java app that's supposed to let people run their Minecraft servers using a program with an interface. When you run a shell script, all output is put in the window, and then you can write input. The input is then read by the process and interpreted. This lets you use sub-commands, such as "start," "stop," "help," "op," and so on. However, I can't figure out how to send the input to the process.
This is what the console looks like:
I tried the following:
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();
}
The field, which I labeled "Input" in the above picture, when accelerated, will execute the actionPerformed() method. However, it seems to freeze at the line: br.read(cmd.toCharArray());
It unfreezes after the process is killed externally. It doesn't even do the System.out part until after it's been killed. So, my question is: how do I do this properly?
EDIT:
After changing it to a writer, it no longer hangs. Instead, it just prints the console message:
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();
}
The writer is accessed like this:
BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
EDIT 2: I've tried messing around with the code, but nothing seems to fix it. I feel like I'm just writing to the wrong stream.