2

I'm trying log all output from an Application in java and for some reason it only capturing the first 2 lines i know the application outputs a lot more than this this is my code

logOut = new BufferedWriter(new FileWriter("WebAdmin.log"));

        ProcessBuilder procBuild = new ProcessBuilder(
            "java", "-Xmx1G", "-Xms512M", "-jar", "TekkitServer\\Tekkit.jar", "nogui", "-nojline"
        );
        server = procBuild.start();

        inputStream = new BufferedReader(new InputStreamReader(server.getInputStream()));
        errorStream = new BufferedReader(new InputStreamReader(server.getErrorStream()));
        outputStream = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));

        String line = "";

        while(!shutdown){
            while((line = inputStream.readLine()) != null){
                logOut.write(line+"\r\n");
                logOut.flush();
                System.out.println(line);
            }
            System.out.println("checking error stream");
            while((line = errorStream.readLine()) != null){
                logOut.write(line+"\r\n");
                logOut.flush();
                System.out.println(line);
            }
        }

        System.out.println("Stoped Reading");
        logOut.close();
        server.destroy();

I'm not even seeing "checking error stream" in my console.

1
  • The other lines might going to the errorstream!!! Commented Mar 23, 2013 at 20:42

2 Answers 2

2

I don't think there is a need for outer while, reading from the output stream blocks until there is some data available, and if the sub process ends you'll get a null and the inner while will break. Also you could use ProcessBuilder's redirectErrorStream(true) to redirect stderr to stdout so you will catch them both in only one loop.

Sign up to request clarification or add additional context in comments.

Comments

1

You must read the stdout and stderr streams in its own thread. Otherwise you will get all kinds of blocking situations (depending on operating system and output patterns).

If you merge the error stream with the output stream (redirectErrorStream(true)), you can get along with a single (additional) background thread. You can avoid this, when you use redirectOutput(File) (or inheritIO).

Comments

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.