zoukankan      html  css  js  c++  java
  • 关于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. }
  • 相关阅读:
    如何使用Doxygen生成keil工程的代码文档 (how to use doxygen properly with keil)
    使用matlab画相交的平面
    转载:关于循环异步操作 Promise 实现,ES7 的 await 和 async
    小众软件:相见恨晚的 Windows 系统下的 cmd 的命令行替代者 Cmder(完美神器)
    Windows系统环境下Python脚本实现全局“划词复制”功能
    Ubuntu shell 命令行路径缩短
    shell查找数组是否有特定的值
    保存数据到文件
    左值与右值
    进程与线程的区别
  • 原文地址:https://www.cnblogs.com/mengziHEHE/p/3160009.html
Copyright © 2011-2022 走看看