Can't get output from Runtime.exec()

2019-08-06 13:27发布

I have written a code to execute a command on shell through Java:

 String filename="/home/abhijeet/sample.txt";

        Process contigcount_p;

        String command_to_count="grep  \">\" "+filename+" | wc -l";

        System.out.println("command for counting contigs "+command_to_count); 

        contigcount_p=Runtime.getRuntime().exec(command_to_count);


         contigcount_p.wait();

As pipe symbols were being used so I was not able to execute command successfully.As per my last question's discussion i have wrapped my variables in shell:

Runtime.getRuntime().exec(new String[]{"sh", "-c", "grep \">\" "+filename+" | wc -l"});

Which worked for me as it does executes command on shell , but still when i try to read its output using buffered reader :

   BufferedReader reader = 
                    new BufferedReader(new InputStreamReader(contigcount_p.getInputStream())); 

   String line=" ";
   while((line=reader.readLine())!=null)
   {
       output.append(line+"\n");
   }

It returns a null value ,I have found a temporary solution for it as i have discussed on previous question: link, but i would like to use right way of doing it by reading it's output using BufferedReader.

标签: java shell
1条回答
该账号已被封号
2楼-- · 2019-08-06 14:23

When I used your command line of {"sh", "-c", "grep \">\" "+filename+" | wc -l"} it kept overriding my file

I had to change it so that the quotes were double quoted, {"sh", "-c", "grep \"\">\"\" "+filename+" | wc -l"}

So, using this as the contents of my test file...

>
>
>

Not a new line >

And using this code...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestProcess {

    public static void main(String[] args) {
        String filename = "test.tx";
        String test = "grep \"\">\"\" "+filename+" | wc -l";
        System.out.println(test);

        try {
            ProcessBuilder pb = new ProcessBuilder("sh", "-c", test);
            pb.redirectError();
            Process p = pb.start();
            new Thread(new Consumer(p.getInputStream())).start();

            int ec = p.waitFor();
            System.out.println("ec: " + ec);
        } catch (IOException | InterruptedException exp) {
            exp.printStackTrace();
        }
    }

    public static class Consumer implements Runnable {

        private InputStream is;

        public Consumer(InputStream is) {
            this.is = is;
        }

        @Override
        public void run() {
            try (BufferedReader reader =  new BufferedReader(new InputStreamReader(is))){
                String value = null;
                while ((value = reader.readLine()) != null) {
                    System.out.println(value);
                }
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

    }

}

I was able to produce this output...

grep "">"" test.tx | wc -l
4
ec: 0

Generally, when dealing with external processes, it's usually easier to use a ProcessBuilder, it has some nice options, including redirecting the error/stdout and setting the execution context directory...

查看更多
登录 后发表回答