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.
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.
Process
object, but rather the subprocess continues executing asynchronously.
Process
object execute asynchronously or concurrently with respect to the Java process that owns the Process
object.
下面就开始举几个简单的示例:
- 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(); -
} -
-
} -
- }
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(); -
} -
} -
} -
-
-
} -
- }
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){ -
} -
} -
-
} -
} - }
- 参考:
- 1 http://wenku.baidu.com/view/17f9a20e52ea551810a687ed
.html
- 2 http://www.cnblogs.com/ChrisWang/archive/2009/12/02/Use-Java-Lang-Process-And-ProcessBuilder-To-Create-Native-Application-Process.html