Start filebeat using a java code?


(Nisal Thiwanka) #1

I'm using filebeat to read some log files and I need to start filebeat using a java program. And the filebeat commands are executed using the windows powerShell. I used the following code but it didn't work.

try {
ProcessBuilder b1 = new ProcessBuilder("powershell.exe", "/c", "cd "C:\Program Files\Filebeat" && ./filebeat -e -c filebeat.yml -d "publish"\");
b1.redirectErrorStream(true);
Process p1 = b1.start();
BufferedReader r1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line1;
while (true) {
line1 = r1.readLine();
if (line1 == null) { break; }
System.out.println(line1);
}

}catch(Exception e) {

}

(Maddin2016) #2

Try this

ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Filebeat\\filebeat.exe", "-c", "C:\\Program Files\\Filebeat\\filebeat.yml", "-e");
pb.redirectErrorStream(true);
Process p = pb.start();

BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;
while ((line = bri.readLine()) != null) {
   System.out.println(line);
}
p.waitFor();

(Nisal Thiwanka) #3

Great. It worked. Thanks a lot..! :slight_smile:


(Nisal Thiwanka) #4

How can I clear the registry file of filebeat using a java code. The relevant poweshell command is as given below.

C:\Program Files\Filebeat>rm data/registry


(Maddin2016) #5

https://docs.oracle.com/javase/tutorial/essential/io/delete.html


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.