1
\$\begingroup\$

I started developing a little multiplayer game with LibGDX and Kryonet. I'm also using the headless backend for the server. So far everything went well, but now I'd like to create a command-line parser for the server. I found out that I should make that in a separate thread, so I made a class for that. But the BufferedReader's readLine() method always returns with null (but it doesn't raise any exceptions). Here is the code I'm using:

public class ServerScreen implements Screen {
    boolean headless;
    Server server;
    CLIParser cliParser = null;

    public ServerScreen(boolean headless) {
        this.headless = headless;
        server = new Server();
        server.start();
        if(headless){
            cliParser = new CLIParser();
            cliParser.start();
        }
        System.out.println("SERVER STARTED");
    }

    //Screen overrides omitted...

    public static class CLIParser implements Runnable{

        public boolean started = false;

        public synchronized void start(){
            if(!started) {
                started = true;
                new Thread(this).start();
            }
        }

        public synchronized void stop(){
            started = false;
        }

        @Override
        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("cliParser run started");
            while(started){
                String line = null;
                try {
                    line = br.readLine();
                } catch (Exception e){
                    System.out.println(e);
                }
                if(line != null){
                    System.out.println("READ LINE: " + line);
                }
            }
        }
    }
}

The cliParser starts up successfully, but the readLine() always returns null, and doesn't even blocks the code from running. I don't get any exceptions. I tried the CLIParser in a dummy project, and it works fine, so I assume this could be an issue with the headless backend.

Here is the sample code I used:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CliParserTester {

    public CliParserTester(){
        CLIParser cp = new CLIParser();
        cp.start();
    }

    public static void main(String[] args) {
        new CliParserTester();
    }

    public class CLIParser implements Runnable{

        public boolean started = false;

        public synchronized void start(){
            if(!started) {
                started = true;
                new Thread(this).start();
            }
        }

        public synchronized void stop(){
            started = false;
        }

        @Override
        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(System.in);
            System.out.println("cliParser run started");
            while(started){
                String line = null;
                try {
                    line = br.readLine();
                    System.out.println(line);
                } catch (Exception e){
                    System.out.println(e);
                }
                if(line != null){
                    System.out.println("READ LINE: " + line);
                }
            }
        }
    }

}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

your gradle build probably didn't hook stdin as your System.in descriptor, try providing it to gradle with:

run{
    standardInput = System.in
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .