https://www.revisitclass.com/java/execute-the-shell-commands-using-processbuilder-in-java/
public static void main(String[] args) throws IOException, InterruptedException {
String homeDirectory = System.getProperty("user.home");
ProcessBuilder builder = new ProcessBuilder("zsh", "-c", "ls");
builder.directory(new File(homeDirectory));
Process result = builder.start();
if (result.waitFor() != 0) {
System.exit(-1);
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(result.getInputStream()));
String output;
while ((output = stdInput.readLine()) != null){
System.out.println(output);
}
}
System.getProperty("user.home")
ProcessBuilder builder = new ProcessBuilder("zsh", "-c", "ls");
builder.directory(new File(homeDirectory));
주의할 점
command는 하나의 문자열로 지정하는 것이 아닌 공백마다 각각 써줘야 한다.
Process result = builder.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(result.getInputStream()));
String output;
while ((output = stdInput.readLine()) != null){
System.out.println(output);
}