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. Oracle session inactive状态临时表数据未清空问题

    问题描述:Oracle数据库,java代码使用某数据库实例,获取connection并在使用结束关闭,而session未销毁,而是状态变为inactive从而导致临时表数据未清空. Oracle临时表 ...

  2. mixamo fuse三维角色制作

    软件下载: http://www.cgtsj.com/cg/yj/1302/index.html 资源名称: Mixamo Fuse三维角色制作软件V1.3版 本站编号:  YJ1302 百度网盘:下 ...

  3. flush tables 好危险啊

    请看图 +----+------+-----------+------+------------+------+-------------------------+------------------ ...

  4. selenium+python笔记5

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 登陆126邮箱 """ f ...

  5. 130. Surrounded Regions -- 被某字符包围的区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. OC 继承子类对象调用方法机制 子类对象访问父类中的实例变量

    在继承中,子类对象如何调用到正确方法的机制 每一个Objective - C对象都有一个隐藏的指针指向类的代码,当向一个对象发送消息的时候,当前的对象会首先在当前类里去查找相应的方法,如果找到的话,直 ...

  7. Tomcat数据库连接池的配置方法总结

    Tomcat数据库连接池的配置方法总结 数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标 ...

  8. 权威第三方报告——获取IT产品竞争力信息的主要途径,类似你买电脑前上的xx论坛看实力评估

    Gartner is known for its huge influence on purchasing as well as an unequaled events business. Forre ...

  9. 陈朱兴-js写法【案例】:

    ajax请求: 一.从服务器端请求数据: var url = '';url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+ ...

  10. 7款适用老旧设备并对初学者非常友好的轻量级Linux发行版

    我们由从 7 到 1 的顺序向大家介绍. 7. Linux Lite 正如其名,Linux Lite 是 Linux 发行版的一个轻量级版本,用户并不需要强大的硬件就可以将它跑起来,而且其使用非常简单 ...