Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:32:20+00:00 2026-06-14T11:32:20+00:00

I’ve spent the past 3 days without much luck on google on how to

  • 0

I’ve spent the past 3 days without much luck on google on how to run a grep process from within Java.

I have the following code to run a grep process, however, I am only getting the first line of the response.

package com.example.parser;


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

public class Main {

    public static void main(String[] args) {
        try {
            Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/").start();

            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

I am only getting the following response:

Binary file /home/user/dev/java/Parser/parser/bin/com/example/parser/Main.class matches
Exit Code: 0

When I should be getting the following response:

Binary file /home/user/dev/java/Parser/parser/com/example/parser/Main.class matches
/home/user/dev/java/Parser/parser/src/com/example/parser/Main.java:10:  public static void main(String[] args) {
/home/user/dev/java/Parser/parser/src/com/example/parser/Main.java:12:          Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
Exit Code: 0

I am wondering why I only get output for the first finding? Is grep forking several processes to run the search and I’m only getting a handle on the first one?


I have also tried running the process from a Thread:

package com.example.parser;

public class Main {

    public static void main(String[] args) {
        try {
            Analyzer analyzer = new Analyzer();
            analyzer.start();
            analyzer.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}


package com.example.parser;

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

public class Analyzer extends Thread {

    public Analyzer() {
    }

    @Override
    public void run() {
        try {
            Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
            process.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

As well as the following:

package com.example.parser;

import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        try {
            Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
            process.waitFor();

            Analyzer analyzer_is = new Analyzer(process.getInputStream());
            Analyzer analyzer_es = new Analyzer(process.getErrorStream());

            analyzer_is.start();
            analyzer_es.start();

            analyzer_is.join();
            analyzer_es.join();

            System.out.println("Exit Code: " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

package com.example.parser;

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

public class Analyzer extends Thread {

    InputStream is = null;

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

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(this.is));

            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

As suggested by the following article: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:32:21+00:00Added an answer on June 14, 2026 at 11:32 am

    I was able to solve the issue by launching a shell with the -c flag. The following code does what I had originally intended:

    package com.example.parser;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
            try {
                List<String> commands = new ArrayList<String>();
                commands.add("/bin/sh");
                commands.add("-c");
                commands.add("grep -rni --include \"*.java\" \"public static void main(\" /home/user/dev/java/Parser/parser");
    
                Process process = new ProcessBuilder(commands).start();
                Analyzer analyzer_is = new Analyzer(process.getInputStream());
                Analyzer analyzer_es = new Analyzer(process.getErrorStream());
    
                analyzer_is.start();
                analyzer_es.start();
    
                process.waitFor();
    
                analyzer_is.join();
                analyzer_es.join();
    
                System.out.println("Exit Code: " + process.exitValue());
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    
    package com.example.parser;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class Analyzer extends Thread {
    
        InputStream is = null;
    
        public Analyzer(InputStream is) {
            this.is = is;
        }
    
        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
    
                String line = "";
                while((line = br.readLine()) != null) {
                      System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.