1

I'm working on an application to act as a 'wrapper' around another another application, in this case I'm working on a wrapper in java to go around a TF2 server. However, I'm running into a rather odd issue with getting the output from the console to appear. Even if I redirect the error stream so that it will go with the input stream, the output to console will cut off after a certain point. I can connect to the server in TF2, so I know for a fact that it's launched.

The code for the wrapper:

public class TF2Wrapper {

public static void main(String[] args) throws IOException {
    ProcessBuilder process = new ProcessBuilder("./srcds_run","+map pl_badwater","+port 27015","2>&1");
    process.redirectErrorStream(true);
    Process server = process.start();
    BufferedReader inputStream = new BufferedReader(new InputStreamReader(server.getInputStream()));
    String s;
    while((s = inputStream.readLine()) != null)
    {
        System.out.println(s);
    }
}

It will read the output up until the second time this line appears: 'Calling BreakpadMiniDumpSystemInit',then no further console output appears, or atleast is picked up by the application.

Is there anything I can do to fix this or is that not possible?

Edit: My guess is it has something to do with the buffering, as trying to do this with python works fine.

1 Answer 1

1

Instead of using BufferedReader use ByteArrayOutputStream

    public class Example {
        public static void main(String[] args) throws IOException,
                InterruptedException {
            InputStream inputStream = null;
            ByteArrayOutputStream arrayOutputStream = null;
//in case of window
            ProcessBuilder builder = new ProcessBuilder("ipconfig");

            try {
                Process process = builder.start();
                inputStream = process.getInputStream();
                byte[] b = new byte[1024];
                int size = 0;
                arrayOutputStream = new ByteArrayOutputStream();

                while ((size = inputStream.read(b)) != -1) {
                    arrayOutputStream.write(b, 0, size);
                }

                System.out.println(new String(arrayOutputStream.toByteArray()));

            } catch (Exception e) {
                e.getStackTrace();
            } finally {
                try {
                    if (inputStream != null)
                        inputStream.close();
                    if (arrayOutputStream != null)
                        arrayOutputStream.close();
                } catch (Exception exception) {
                    exception.getStackTrace();
                }
            }
    }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

This didn't do anything. Now it doesn't even output anything.
@VoidWhisperer please see the changes now i have added full class.And for TF2 server is your command is running ? if yes what is the status code you are getting u can get the status code by this int x = process.exitValue(); if x=0 then ur command run successfully.In your case may be command is not running
With the exact code above it still prints absolutely nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.