Question

I am trying to write a small Java application that will let me run a Bukkit server off-screen using the Java Process/ProcessBuilder API.

I am able to get the output from the server fine, but the server doesn't respond to commands written by the output stream returned by Process.getOutputStream() (chained to the process input stream).

I tried doing this with my own test code, and it worked. The separate process reading from System.in received the text written to the output stream.

Does Bukkit not listen to System.in or something? If not, how can that be? Any ideas?

try {
    ProcessBuilder pb = new ProcessBuilder();
    File dir = new File("C:/Users/Brian/Desktop/MC-Server/Bukkit-Testing");
    pb.directory(dir);
    pb.command(new String[] {"java", "-Xincgc", "-Xmx1G", "-jar", "craftbukkit-1.0.1-R1.jar"});
    pb.redirectErrorStream(true);
    final Process p = pb.start();
    InputStream out = p.getInputStream();
    BufferedReader r1 = new BufferedReader(new InputStreamReader(out));
    String s = null;
    new Thread(new Runnable() {

        @Override
        public void run() {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            Scanner scan = new Scanner(System.in);
            String input = null;
            while((input=scan.nextLine()) != null) {
                if(input.equals("exit")) {
                    p.destroy();
                    break;
                }
                try {
                    bw.write(input);
                    bw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }).start();
    while((s=r1.readLine()) !=null)
        System.out.println(s);
} catch (IOException e) {
    e.printStackTrace();
}
Was it helpful?

Solution

I don't think Bukkit uses its System.in, so we have to make a workaround.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class ConsolePlugin extends JavaPlugin {

    public Logger log;

    public void onEnable(){
        log = this.getLogger();
        log.info("BufferedReader has been enabled!");
        new Thread(new Runnable(){
            public void run(){
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String line = null;
                while (true){
                    try {
                        line=br.readLine();
                    } catch (Exception e) {e.printStackTrace();}
                    if (line!=null){
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), line);
                        System.out.println(line);
                    }
                }
            }
        }).start();
    }

    public void onDisable(){
        log.info("BufferedReader has been disabled.");
    }
}

To send commands:

bw.write(input);
bw.nextLine();
bw.flush();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top