Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: why is terminal not returning results to ide

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default why is terminal not returning results to ide

    hi i am trying to run a command in terminal the code is below if i run the command in terminal it works fine however when i run it from netbeans with code below nothing gets printed. however if i run a different command such as (ip addr) it works fine any ideas on why?

    public static void a() throws IOException{
                ArrayList lister=new ArrayList();
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("ps -ef | grep firefox");// the command i am trying to run to get pid of application
                InputStream stderr = proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line;
                while ( (line = br.readLine()) != null){
                    lister.add(line);
                }
                for(int y=0;y<lister.size();y++){
                    System.out.println(lister.get(y));
                }
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why is terminal not returning results to ide

    Pipe's have issues. Three options:
    a) Execute directly with a shell (eg sh -c "ps -ef | grep firefox")
    b) write a shell script and call that script directly
    c) Do the commands one at a time, using the Input/Output streams to manually pipe the results of one to another.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: why is terminal not returning results to ide

    Note that for option (a) the commands need to be put into a String array, e.g.,
    Process proc = rt.exec(new String[] { "/bin/sh", "-c", "ps -ef | grep firefox" });
     
    // Doesn't work - no output.
    // Process proc = rt.exec("/bin/sh -c \"ps -ef | grep firefox\"");

    Btw I noticed that you had this comment, "the command i am trying to run to get pid of application." Depending on the OS you're using (you should have it if you're using Solaris, Linux or BSD), "pgrep" may be the better command to use.
    Last edited by jashburn; May 14th, 2014 at 08:46 PM. Reason: Add note of pgrep

  4. The Following User Says Thank You to jashburn For This Useful Post:

    bean (May 15th, 2014)

  5. #4
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: why is terminal not returning results to ide

    Process proc = rt.exec(new String[] { "/bin/sh", "-c", "ps -ef | grep firefox" });
    this solution worked thanks for replys

Similar Threads

  1. Replies: 3
    Last Post: May 25th, 2014, 11:33 AM
  2. Help for Terminal Exams................
    By arko_chatterjee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 21st, 2013, 09:31 AM
  3. get terminal value back to ide?
    By bean in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 23rd, 2013, 07:01 PM
  4. Replies: 0
    Last Post: March 1st, 2013, 08:13 PM
  5. Terminal Tamagotchi
    By FlurrYx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2011, 01:05 PM