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. alertdialog.builder 自定义弹窗

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  2. Tiny PXE Server简介

    Tiny PXE Server简介Tiny PXE Server是一款小巧而功能强大的网启软件.支持DHCP TFTP HTTP BINL DNS等多个协议,支持grub4dos,pxelinux,i ...

  3. jsp 页面 jstl 日期的计算

    1. <% page import ="java.util.Date"%> 2. <% Date date = new Date(); date = new Da ...

  4. aspcms网站访问出现3706错误, 错误描述:未找到提供程序。该程序可能未正确安装,解决的方法。

       

  5. 3.5 EF Code First总结

    1. 主键约定 属性名为“ID”(不区分大小写)或类名的后面跟有“ID”. 2. 关系约定 模型之间的关系,EF根据针对类型定义的导航属性来推断关系. 3. 连接字符串约定 (1)默认配置 如果连接字 ...

  6. java SE学习之线程同步(详细介绍)

           java程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题:               当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...

  7. 初学java之触发响应事件

    import java.awt.*; import javax.swing.*; import java.awt.event.*; class WindowActionEvent extends JF ...

  8. jdbc URL中的各个参数详解

    常用的有两个,一个是gjt(Giant JavaTree)组织提供的mysql驱动,其JDBC Driver名称(Java类名)为:org.gjt.mm.mysql.Driver 详情请参见网站:ht ...

  9. 安卓手机修改host

    电脑修改 注意:usb设置为调试模式 1.手机必须先root,小米可以安卓开发版系统即可 2.安卓 adb工具(android debug bridge) 3.依次执行下面的命令 1.adb root ...

  10. struts2视频学习笔记 11-12(动态方法调用,接收请求参数)

    课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...