0

I have a code which calls a specific command within the Mac Terminal, I then use a buffered reader to read and print the result from the terminal. I have been doing this with a couple of commands, only problem is I am now in need of using the command ps -ef | grep google (program name is of course different, but for now I'll just use google). The only issue by using this command is the fact that the buffered reader doesn't seem to be able to read and print the outcome. I am 100% sure that the actual command works, as I have tried to run the command in the terminal alone. I am not exactly sure why this problem is all of a sudden happening now.

The command is used to check whether a specific application is running and where it has been launched from.

The code I am using is the one below here.

try {
            String procss;
            Process pRun = Runtime.getRuntime().exec("ps -ef | grep google");
            BufferedReader input = new BufferedReader(new InputStreamReader(pRun.getInputStream()));
            while ((procss = input.readLine()) != null) {
                if(!procss.contains("Contents"))
                {
                    //Do something
                }
            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }

To give an example of a command where it has worked without any problem, I have also just included a working code below. To show how I would actually like it to work, but just with the right command ps -ef | grep google instead.

try {
                String procss;
                Process pRun = Runtime.getRuntime().exec("top -F -R -o cpu");
                BufferedReader input = new BufferedReader(new InputStreamReader(pRun.getInputStream()));
                while ((procss = input.readLine()) != null) {
                    if(!procss.contains("testApplication"))
                    {
                        //Do something
                    }
                }
                input.close();
            } catch (Exception err) {
                err.printStackTrace();
            }

I am not sure whether I need to use another method of reading the outcome or what I have to do for my code to actually work again.

  • Possible duplicate of [How to use Pipe Symbol through exec in Java](http://stackoverflow.com/questions/7226538/how-to-use-pipe-symbol-through-exec-in-java) – Adonis Mar 11 '17 at 18:33
  • Actually as mentioned in the link above, that is simply because you are not running a shell with *Runtime.exec*, you are spawning a process – Adonis Mar 11 '17 at 18:36
  • Thank you very much for the input, I used the second answer on the duplicate question. The help is much appreciated! –  Mar 11 '17 at 19:46

0 Answers0