Java by API/java.lang/ProcessBuilder — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:41, 31 мая 2010

ProcessBuilder: command(String... command)

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Map;
public class MainClass {
  public static void main(String args[]) throws Exception {
    ProcessBuilder launcher = new ProcessBuilder();
    Map<String, String> environment = launcher.environment();
    launcher.redirectErrorStream(true);
    launcher.directory(new File("c:\\"));
    environment.put("name", "var");
    launcher.rumand("notepad.exe");
    Process p = launcher.start(); // And launch a new process
    BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = output.readLine()) != null)
      System.out.println(line);
    // The process should be done now, but wait to be sure.
    p.waitFor();
  }
}





ProcessBuilder: directory(File directory)

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Map;
public class MainClass {
  public static void main(String args[]) throws Exception {
    ProcessBuilder launcher = new ProcessBuilder();
    Map<String, String> environment = launcher.environment();
    launcher.redirectErrorStream(true);
    launcher.directory(new File("c:\\"));
    environment.put("name", "var");
    launcher.rumand("notepad.exe");
    Process p = launcher.start(); // And launch a new process
    BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = output.readLine()) != null)
      System.out.println(line);
    // The process should be done now, but wait to be sure.
    p.waitFor();
  }
}





ProcessBuilder: environment()

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Map;
public class MainClass {
  public static void main(String args[]) throws Exception {
    ProcessBuilder launcher = new ProcessBuilder();
    Map<String, String> environment = launcher.environment();
    launcher.redirectErrorStream(true);
    launcher.directory(new File("c:\\"));
    environment.put("name", "var");
    launcher.rumand("notepad.exe");
    Process p = launcher.start(); // And launch a new process
    BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = output.readLine()) != null)
      System.out.println(line);
    // The process should be done now, but wait to be sure.
    p.waitFor();
  }
}





ProcessBuilder: redirectErrorStream(boolean redirectErrorStream)

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Map;
public class MainClass {
  public static void main(String args[]) throws Exception {
    ProcessBuilder launcher = new ProcessBuilder();
    Map<String, String> environment = launcher.environment();
    launcher.redirectErrorStream(true);
    launcher.directory(new File("c:\\"));
    environment.put("name", "var");
    launcher.rumand("notepad.exe");
    Process p = launcher.start(); // And launch a new process
    BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = output.readLine()) != null)
      System.out.println(line);
    // The process should be done now, but wait to be sure.
    p.waitFor();
  }
}





ProcessBuilder: start()

/*
 * Output: 
 */
public class MainClass {
  public static void main(String args[]) {
    try {
      ProcessBuilder proc =  new ProcessBuilder("notepad.exe testfile");
      proc.start();
    } catch (Exception e) {
      System.out.println("Error executing notepad.");
    }
  }
}