Show how to use exec to pass complex args : UNIX Win32 « Development Class « Java






Show how to use exec to pass complex args

  

import java.io.*;

/*
 * Show how to use exec to pass complex args (which are almost
 * certainly system-dependant) to a command-line interpreter.
 */
public class ExecShellArgs {
  public static void main(String[] args) throws IOException {
    Runtime r = Runtime.getRuntime();
    String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
    Process p = r.exec(nargs);
    BufferedReader is =
      new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = is.readLine()) != null)
      System.out.println(line);
  }
}


           
         
    
  








Related examples in the same category

1.Java 1.5 (5.0) Changes to the API: ProcessBuilder.
2.How to execute a program from within Java
3.How to execute an external program How to execute an external program
4.ExecDemo shows how to execute an external program 2
5.ExecDemo shows how to execute an external program
6.Execute an external program read its output, and print its exit status
7.Create some temp files, ls them, and rm them
8.ExecDemoHelp shows how to use the Win32 start command
9.ExecDemo shows how to execute an external program and read its output
10.ExecDemo shows how to execute an external program and read its output 3
11.UNIX getopt() system call
12.Unix Crypt
13.Handles program arguments like Unix getopt()
14.Helper method to execute shell command
15.dealing with Excel dates