Search notes:

Java class java.lang.Runtime - exec

The method exec of java.lang.Runtime starts a new process and returns a java.lang.Process
The following example creates a cmd.exe process and executes dir /s /b ..\.. to list all files beneath the directory that is two levels higher than the current one. It then reads the process' stdout and repeats it to the console:
public class exec {

   public static void main(String[] argv) {

      java.lang.Runtime rt = java.lang.Runtime.getRuntime();

      try {

      // java.lang.Process proc   = rt.exec("c:\\Program Files\\Mozilla Firefox\\firefox.exe http://renenyffenegger.ch/");
         java.lang.Process proc   = rt.exec("cmd.exe /c dir /s /b ..\\..");

      // proc.waitFor();

      //
      // Note: «StdOut» is returned by getInputStream() | and «stdIn» is returned by «getOutputStream»...
      //
         java.util.Scanner stdOut = new java.util.Scanner(proc.getInputStream()).useDelimiter("\\n");
      // java.util.Scanner stdErr = new java.util.Scanner(proc.getErrorStream()).useDelimiter("\\n");

         while (stdOut.hasNext() && proc.isAlive()) {
            System.out.println("stdout: " + stdOut.next());
         }
      // while (stdErr.hasNext()) {
      //    System.out.println("stdErr: " + stdErr.next());
      // }
         
      }
      catch (java.lang.Exception ex) {
         System.out.println(ex.toString());
      }
   }
}
Github repository JavaClasses, path: /java/lang/Runtime/exec.java

See also

Java classes

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759373922, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Java/classes/java/lang/Runtime/exec(72): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78