如何使用Java运行并返回cassandra nodetool命令及其输出。我检查了this SO question,但没有明确说明如何做到这一点。
答案 0 :(得分: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();
}
}