How to execute an external program : UNIX Win32 « Development Class « Java






How to execute an external program

How to execute an external program
  
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.io.IOException;

/**
 * ProcessBuilderDemo shows how to execute an external 
 * program (in this case the MS-Windows notepad program).
 */
public class ProcessBuilderDemo {

  public static void main(String argv[]) 
  throws InterruptedException, IOException {
    
    List<String> command = new ArrayList<String>();
    command.add("notepad");
    command.add("foo.txt");
    ProcessBuilder builder = new ProcessBuilder(command);
    Map<String, String> environ = builder.environment();
    environ.put("PATH", "/windows;/windows/system32;/winnt");
    builder.directory(
      new File(System.getProperty("user.home")));

    final Process godot = builder.start();
    
    godot.waitFor();

    System.out.println("Program terminated!");
    return;
  }
}



           
         
    
  








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.Show how to use exec to pass complex args
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