Running grep from a Java program

I spent the last 3 days without much luck on google on how to start a grep process from Java.

I have the following code to start a grep process, however I only get the first line of 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 get the following response:

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

When I get the following answer:

 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 output? Is grep expanding multiple processes to start a search and I only get a handle on the first?


I also tried to start the process from the 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(); } } } 

Like 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 in the following article: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

+4
source share
4 answers

I managed to solve the problem by running the shell with the -c flag. The following code does what I originally planned:

 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(); } } } 
+2
source

Perhaps this is due to the fact that you are not waiting for the end of grep.

Use the waitFor method :

 Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/").start(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); process.waitFor(); String line = ""; while((line = br.readLine()) != null) { System.out.println(line); } 

Please note that you can also read the output (mainly to get what happens) while it is being processed using

 Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", String line; while (true) { line = reader.readLine(); // add IO exception catching if (line != null) { System.out.println(line); } else { Thread.sleep(DELAY); // DELAY could be 100 (ms) for example } } 

I am sure that you are sure that grep, launched by the owner of the java program, has a length of more than one line?

+1
source

Another reason might be that your process is still running, but your Java program has just quit.

Use process.waitFor (); and Read your input stream in the stream.

Run the process. Lunch stream with process input as input. Wait for the process to complete using process.waitFor ();

This can help!

0
source

Have a look at this project for grep in java https://code.google.com/p/grep4j

0
source

Source: https://habr.com/ru/post/1446269/


All Articles