Java by API/java.lang/ProcessBuilder

Материал из Java эксперт
Версия от 14:41, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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.");
    }
  }
}