1

I am trying to run split command in a Java program. When I run it in the console with argument --verbose, it prints the generated chunk as follows:

creating file 'chunk00'
creating file 'chunk01'
creating file 'chunk02'

But when I run it in a java program, these outputs will be printed after finishing the process. What I must do to get outputs while split process is running?

I've used the following code:

ProcessBuilder pb = new ProcessBuilder("split", "-a 2", "-d", "-b 52MB","--verbose",path+"/"+db,"chunk");
        pb.redirectErrorStream(true);
        File workingFolder = new File("/home/hajibaba");
        pb.directory(workingFolder);
        Process proc = pb.start();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

        // read the output from the command
        String s = null;
        while ((s = stdInput.readLine()) != null)
        {
            System.out.println(s);
        }
        proc.waitFor();

However, it works for a bash script that uses echo for print same results.

1
  • 1
    You must also split the "-a 2" to "-a", "2", also "-b 52MB" to "-b", "52MB" Commented Mar 1, 2017 at 11:24

1 Answer 1

1

I/O is line buffered in an interactive session but buffered when writing to a pipe.

To work around the problem you can turn off buffering with the unbuffer command.

ProcessBuilder pb = new ProcessBuilder("unbuffer", "split", "-a2", "-d", "-b52MB","--verbose",path+"/"+db,"chunk");

https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe

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

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.