http://docs.oracle.com/javase/1.5.0/docs/api/
The ProcessBuilder.start()
and Runtime.exec
methods create a native process and return an instance of a subclass of Process
that can be used to control the process and obtain information about it. The class Process
provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream()
, getInputStream()
, getErrorStream()
). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
The subprocess is not killed when there are no more references to the Process
object, but rather the subprocess continues executing asynchronously.
There is no requirement that a process represented by a Process
object execute asynchronously or concurrently with respect to the Java process that owns the Process
object.
下面就开始举几个简单的示例:
(1)执行简单的DOS命令,如打开一个记事本
- package com.iwtxokhtd.other;
- import java.io.IOException;
- public class ProcessTest {
- public static void main(String[] args) {
- try {
- Process proc=Runtime.getRuntime().exec("notepad");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
(2)使用它的其它构造方法执行相关的命令,如下例:
- package com.iwtxokhtd.other;
- import java.io.IOException;
- public class ProcessTest {
- public static void main(String[] args) {
- try {
- String exeFullPathName="C:/Program Files/Internet Explorer/IEXPLORE.EXE";
- String message="www.google.com";
- String []cmd={exeFullPathName,message};
- Process proc=Runtime.getRuntime().exec(cmd);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
执行上述命令可以打开Google网站
(3)列出系统正在运行的所有进程信息
- package com.iwtxokhtd.other;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ListAllProcessTest {
- //列出所有的进程信息
- public static void main(String[] args) {
- BufferedReader br=null;
- try {
- Process proc=Runtime.getRuntime().exec("tasklist");
- br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
- @SuppressWarnings("unused")
- String line=null;
- System.out.println("打印所有正在运行的进程信息");
- while((line=br.readLine())!=null){
- System.out.println(br.readLine());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(br!=null){
- try {
- br.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
(4)判断一个具体的进程是否正在运行,如下例:
- package com.iwtxokhtd.other;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class FindProcessExeTest
- {
- public static void main(String []args){
- if(findProcess("QQ.exe")){
- System.out.println("------判断指定的进程是否在运行------");
- System.out.println("QQ.exe该进程正在运行!");
- }else{
- System.out.println("------判断指定的进程是否在运行------");
- System.out.println("QQ.exe该进程没有在运行!");
- }
- }
- public static boolean findProcess(String processName){
- BufferedReader br=null;
- try{
- //下面这句是列出含有processName的进程图像名
- Process proc=Runtime.getRuntime().exec("tasklist /FI /"IMAGENAME eq "+processName+"/"");
- br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
- String line=null;
- while((line=br.readLine())!=null){
- //判断指定的进程是否在运行
- if(line.contains(processName)){
- return true;
- }
- }
- return false;
- }catch(Exception e){
- e.printStackTrace();
- return false;
- }finally{
- if(br!=null){
- try{
- br.close();
- }catch(Exception ex){
- }
- }
- }
- }
- }