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命令,如打开一个记事本

  1. package com.iwtxokhtd.other;
  2. import java.io.IOException;
  3. public class ProcessTest {
  4. public static void main(String[] args) {
  5. try {
  6. Process proc=Runtime.getRuntime().exec("notepad");
  7. catch (IOException e) {
  8. // TODO Auto-generated catch block
  9. e.printStackTrace();
  10. }
  11. }
  12. }

(2)使用它的其它构造方法执行相关的命令,如下例:

Java代码 
  1. package com.iwtxokhtd.other;
  2. import java.io.IOException;
  3. public class ProcessTest {
  4. public static void main(String[] args) {
  5. try {
  6. String exeFullPathName="C:/Program Files/Internet Explorer/IEXPLORE.EXE";
  7. String message="www.google.com";
  8. String []cmd={exeFullPathName,message};
  9. Process proc=Runtime.getRuntime().exec(cmd);
  10. catch (IOException e) {
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. }
  14. }
  15. }

执行上述命令可以打开Google网站

(3)列出系统正在运行的所有进程信息

Java代码 
  1. package com.iwtxokhtd.other;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. public class ListAllProcessTest {
  6. //列出所有的进程信息
  7. public static void main(String[] args) {
  8. BufferedReader br=null;
  9. try {
  10. Process proc=Runtime.getRuntime().exec("tasklist");
  11. br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
  12. @SuppressWarnings("unused")
  13. String line=null;
  14. System.out.println("打印所有正在运行的进程信息");
  15. while((line=br.readLine())!=null){
  16. System.out.println(br.readLine());
  17. }
  18. catch (IOException e) {
  19. e.printStackTrace();
  20. }finally{
  21. if(br!=null){
  22. try {
  23. br.close();
  24. catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }
  30. }

(4)判断一个具体的进程是否正在运行,如下例:

Java代码 
    1. package com.iwtxokhtd.other;
    2. import java.io.BufferedReader;
    3. import java.io.InputStreamReader;
    4. public class FindProcessExeTest
    5. {
    6. public static void main(String []args){
    7. if(findProcess("QQ.exe")){
    8. System.out.println("------判断指定的进程是否在运行------");
    9. System.out.println("QQ.exe该进程正在运行!");
    10. }else{
    11. System.out.println("------判断指定的进程是否在运行------");
    12. System.out.println("QQ.exe该进程没有在运行!");
    13. }
    14. }
    15. public static boolean findProcess(String processName){
    16. BufferedReader br=null;
    17. try{
    18. //下面这句是列出含有processName的进程图像名
    19. Process proc=Runtime.getRuntime().exec("tasklist /FI /"IMAGENAME eq "+processName+"/"");
    20. br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
    21. String line=null;
    22. while((line=br.readLine())!=null){
    23. //判断指定的进程是否在运行
    24. if(line.contains(processName)){
    25. return true;
    26. }
    27. }
    28. return false;
    29. }catch(Exception e){
    30. e.printStackTrace();
    31. return false;
    32. }finally{
    33. if(br!=null){
    34. try{
    35. br.close();
    36. }catch(Exception ex){
    37. }
    38. }
    39. }
    40. }
    41. }

关于process的更多相关文章

  1. IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页

    因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...

  2. C#的Process类调用第三方插件实现PDF文件转SWF文件

    在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...

  3. C# ShellExcute与Process

    C#运行外部程序的两种方法 ShellExecute using System.Runtime.InteropServices; public enum ShowWindowCommands : in ...

  4. 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方

    刚上线一个新版本,其中有台电脑打开软件就报[xx的类型初始值设定项引发异常](还好不是一大波电脑,新东西上线就怕哀鸿遍野),如图: 显然是该类型的静态构造函数中抛异常了(红线处就是类名),遂打开该类, ...

  5. C# - 多线程 之 Process与Thread与ThreadPool

    Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...

  6. Java Business Process Management(业务流程管理) 初识环境搭建

    一.简介 (一)什么是jbpm JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易 ...

  7. Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details

    thinkphp 在Apache上配置启用伪静态,重启Apache1 restart 竟然失败了,报错 Job for httpd.service failed because the control ...

  8. 更新过程 renewal process

    一类随机过程.是描述元件或设备更新现象的一类随机过程.设对某元件的工作进行观测.假定元件的使用寿命是一随机变量,当元件发生故障时就进行修理或换上新的同类元件,而且元件的更新是即时的(修理或更换元件所需 ...

  9. Transaction (Process ID xxx) was deadlocked on lock

    Transaction (Process ID 161) was deadlocked on lock | communication buffer resources with another pr ...

  10. NodeJS入门(五)—— process对象

    process对象用于处理与当前进程相关的事情,它是一个全局对象,可以在任何地方直接访问到它而无需引入额外模块. 它是 EventEmitter 的一个实例. 本章的示例可以从我的Github上下载到 ...

随机推荐

  1. HTML5游戏实战(4): 20行代码实现FlappyBird

    这个系列很久没有更新了.几个月前有位读者调侃说,能不能一行代码做一个游戏呢.呵呵,接下来一段时间,我天天都在想这个问题,怎么能让GameBuilder+CanTK进一步简化游戏的开发呢.经过几个月的努 ...

  2. 日期操作类--SimpleDateFormat类

    使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...

  3. 20145218 《Java程序设计》第三周学习总结

    20145218 <Java程序设计>第三周学习总结 教材学习内容总结 定义类 编写程序要产生对象就要先定义类.类是对象的设计图,对象是类的实例.类定义时使用class关键词,建立实例时, ...

  4. node.js应用Redis数据库

    node.js下使用Redis,首先: 1.有一台安装了Redis的服务器,当然,安装在本机也行 2.本机,也就是客户端,要装node.js 3.项目要安装nodejs_redis模块 注意第 3 点 ...

  5. ubuntu下安装基本配置

    安装ubuntu更新: sudo apt-get update sudo apt-get upgrade 安装Docky: sudo add-apt-repository ppa:ricotz/doc ...

  6. (19)odoo中的javascript

    -----------更新日期15:17 2016-02-16 星期二-----------* 用到的js库   我们可以打开 addons/web/views/webclient_template. ...

  7. Testin

    http://www.testin.cn/ http://news.ccidnet.com/art/66/20150416/5815927_1.html 百度百科上面的   Testin是全球最大的移 ...

  8. Android: Intent实现活动之间的交互

    Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...

  9. Apache网页有时能访问,有时超时打不开

    在win server 2008上安装wamp2.4版本,配置apache 访问网页一直在加载,似乎被挂起. 转圈需要3分钟多钟, 最终显示无法访问. 或者超时. 错误日志中有如下提示: [Sat J ...

  10. 老男孩linux高级架构 百度云盘下载

    关于Linux的资源我了解还是比较少的,因为我最讨厌用命令行了(那是我大学时代的阴影啊!).这个资源收集很久了一直没有分享出来,因为我对Linux的了解真的很少,不知道怎么去描述,但是今天分享出来我想 ...