If anyone is looking for a solution for macOS, I made one:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CustomDesktop {
public static void main(String[] args) throws IOException, InterruptedException {
// Execute multiple commands in one terminal.
final String[] TERMINAL_COMMANDS = {"cd ~/Videos", "ls -l"};
StringBuilder command = new StringBuilder(); // Using StringBuilder is recommended: less memory and faster.
for (String terminalCommand : TERMINAL_COMMANDS) {
command.append(terminalCommand).append(';');
}
execute(command.toString());
// Direct method call, if you prefer.
execute("cd ~/Videos; ls -l");
}
public static void execute(String command) throws IOException, InterruptedException {
// Use "bash" instead if your Mac runs pre-Catalina.
ProcessBuilder builder = new ProcessBuilder("zsh", "-c", command);
Process process = builder.inheritIO().start();
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
}
}
Output:
total 539296
-rw-r--r--@ 1 macintoshfan staff 276116282 Jun 18 14:14 Java.mov
total 539296
-rw-r--r--@ 1 macintoshfan staff 276116282 Jun 18 14:14 Java.mov