• 关于process


    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.   
    3. import java.io.IOException;   
    4.   
    5. public class ProcessTest {   
    6.   
    7.     public static void main(String[] args) {   
    8.         try {   
    9.                         Process proc=Runtime.getRuntime().exec("notepad");   
    10.         } catch (IOException e) {   
    11.             // TODO Auto-generated catch block   
    12.             e.printStackTrace();   
    13.         }   
    14.   
    15.     }   
    16.   
    17. }  

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

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

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

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

      

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

    (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.            
      8.         if(findProcess("QQ.exe")){   
      9.             System.out.println("------判断指定的进程是否在运行------");   
      10.             System.out.println("QQ.exe该进程正在运行!");   
      11.         }else{   
      12.             System.out.println("------判断指定的进程是否在运行------");   
      13.             System.out.println("QQ.exe该进程没有在运行!");   
      14.         }   
      15.   
      16.     }   
      17.     public static boolean findProcess(String processName){   
      18.         BufferedReader br=null;   
      19.         try{   
      20.               
      21.             //下面这句是列出含有processName的进程图像名   
      22.             Process proc=Runtime.getRuntime().exec("tasklist /FI /"IMAGENAME eq "+processName+"/"");   
      23.             br=new BufferedReader(new InputStreamReader(proc.getInputStream()));   
      24.             String line=null;   
      25.             while((line=br.readLine())!=null){   
      26.                 //判断指定的进程是否在运行   
      27.                 if(line.contains(processName)){   
      28.                     return true;   
      29.                 }   
      30.             }   
      31.                
      32.             return false;   
      33.         }catch(Exception e){   
      34.             e.printStackTrace();   
      35.             return false;   
      36.         }finally{   
      37.             if(br!=null){   
      38.                 try{   
      39.                     br.close();   
      40.                 }catch(Exception ex){   
      41.                 }   
      42.             }   
      43.                
      44.         }   
      45.     }   
      46. }
  • 相关阅读:
    用户体验的时间尺度
    ibatis的xml中的dtd问题
    ASP.NET AJAX调用服务
    C#打包安装与卸载
    学习 WCF (6)学习调用WCF服务的各种方法
    C#.NET ActiveX控件的制作
    如何使用C#开发“类ActiveX组件”
    Asp.Net框架下WebService和Remoting的区别
    今天打开网站,突然发现sql 2005出现错误:数据库 'mybase_db' 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。
    VS部署中的ProductCode问题
  • 原文地址:https://www.cnblogs.com/mengziHEHE/p/3160009.html
Copyright © 2020-2023  润新知