Java by API/java.lang/ProcessBuilder
Содержание
ProcessBuilder: command(String... command)
<source lang="java">
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(); }
}
</source>
ProcessBuilder: directory(File directory)
<source lang="java">
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(); }
}
</source>
ProcessBuilder: environment()
<source lang="java">
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(); }
}
</source>
ProcessBuilder: redirectErrorStream(boolean redirectErrorStream)
<source lang="java">
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(); }
}
</source>
ProcessBuilder: start()
<source lang="java">
/*
* 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."); } }
}
</source>