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