2

I have read many examples and ended up using the following code to execute a command line command from inside of a Java program.

public static void executeCommand(final String command) throws IOException, 
    InterruptedException {
        System.out.println("Executing command " + command);
        final Runtime r = Runtime.getRuntime();
        final Process p = r.exec(command);
        System.out.println("waiting for the process");
        p.waitFor();
        System.out.println("waiting done");
        try (final BufferedReader b = new BufferedReader(new InputStreamReader(
            p.getInputStream()))) {
            String line;

            while ((line = b.readLine()) != null) {
                System.out.println(line);
            }
        }
    }

I have tested it with a simple ls command and it works fine. When I try to run another command, it is taking forever (kept running for 25 minutes and did not stop yet).

When I execute a tabix command on the command line, I get the following statistics

4.173u 0.012s 0:04.22 99.0% 0+0k 0+0io 0pf+0w

Hence it should finish fast.

The command is

time tabix file pos1 pos2 ... pos190 > /dev/null

Could the problem be that the tabix command includes > /dev/null at the end? If not, what could cause this issue?

Tad
  • 838
  • 2
  • 11
  • 22
  • What is "forever"? Have you tried other commands, both simple and difficult? There probably is an extra delay depending on what is in the command. – Juru Oct 01 '14 at 20:44
  • 2
    More details, please. What is the exact command you're running? What are the timings for running the command directly, and what are the timings for a `public static void main` that just executes this command and then terminates? – chiastic-security Oct 01 '14 at 20:44
  • The command has been running for 14 minutes and is still running. – Tad Oct 01 '14 at 20:45
  • the command is basically time tabix file pos1 pos2 pos3 ... pos190 > /dev/null – Tad Oct 01 '14 at 20:46
  • I've had a similar problem with an execution with parameters, take a look at this answer: http://stackoverflow.com/a/25082093/869783 – MarcioB Oct 01 '14 at 20:56
  • what if I am not allowed to run sudo? – Tad Oct 01 '14 at 21:10
  • 1
    You need to attach the reader to the process **before** calling it's `waitFor`. Without that it will fill it's allocated output buffer and then block - but only for big output, small output will seem to be fine. – OldCurmudgeon Oct 01 '14 at 21:15
  • @OldCurmudgeon thank you! that had fixed it! One more question - why is the redirection to dev/null still displayed? – Tad Oct 01 '14 at 21:18
  • @tad - what does it print - there should be a hint there. – OldCurmudgeon Oct 01 '14 at 21:24

1 Answers1

2

You need to attach the reader to the process before calling it's waitFor. Without that it could fill it's allocated output buffer and then block - but only for big output, small (e.g. test) output will seem to be fine.

public static void executeCommand(final String command) throws IOException, InterruptedException {
    System.out.println("Executing command " + command);
    // Make me a Runtime.
    final Runtime r = Runtime.getRuntime();
    // Start the command process.
    final Process p = r.exec(command);
    // Pipe it's output to System.out.
    try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
        String line;

        while ((line = b.readLine()) != null) {
            System.out.println(line);
        }
    }
    // Do this AFTER you've piped all the output from the process to System.out
    System.out.println("waiting for the process");
    p.waitFor();
    System.out.println("waiting done");
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213