fork(7) download
  1. import java.io.*;
  2.  
  3. class LinuxInteractor {
  4.  
  5. static public void main(String[] args) {
  6. String[] cmdArr = {"ls", "-l"};
  7. LinuxInteractor.executeCommand(cmdArr);
  8. }
  9.  
  10. static public void executeCommand(String[] commandArr) {
  11. System.out.println("Linux command: "
  12. + java.util.Arrays.toString(commandArr));
  13.  
  14. try {
  15. ProcessBuilder pb = new ProcessBuilder(commandArr);
  16. pb.redirectErrorStream(true);
  17. Process proc = pb.start();
  18. proc.getInputStream()));
  19. System.out.println("Process started !");
  20.  
  21. String line;
  22. while ((line = in.readLine()) != null) {
  23. System.out.println(line);
  24. }
  25.  
  26. proc.destroy();
  27. System.out.println("Process ended !");
  28. } catch (Exception x) {
  29. x.printStackTrace();
  30. }
  31. }
  32. }
Success #stdin #stdout 0.07s 380288KB
stdin
Standard input is empty
stdout
Linux command: [ls, -l]
Process started !
total 4
-rw-r--r-- 1 0 110 1981 Jun 11 09:33 tested.zip
Process ended !