How can i run and return the cassandra nodetool command and its output using Java. I checked this SO question but there is no clarity about how to do it exactly.
Asked
Active
Viewed 182 times
2
-
What it's your use case? May be you can get out those info from JMX or run some commands through JMX. – Horia Jan 31 '18 at 07:57
-
i want to run any nodetool command in java and get the ouput – Manish Kumar Jan 31 '18 at 08:05
1 Answers
1
import java.io.InputStreamReader;
public class ShellTest {
public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
// Get runtime
java.lang.Runtime rt = java.lang.Runtime.getRuntime();
// Start a new process: UNIX command ls
java.lang.Process p = rt.exec("ccm node1 nodetool status");
// You can or maybe should wait for the process to complete
p.waitFor();
System.out.println("Process exited with code = " + p.exitValue());
// Get process' output: its InputStream
java.io.InputStream is = p.getInputStream();
java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
// And print each line
String s = null;
while ((s = reader.readLine()) != null) {
System.out.println(s);
}
is.close();
}
}

Horia
- 2,942
- 7
- 14