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. English substitute

    英语写作中替换掉用到发腻的↓常用词↓,吐血整理2小时~~   动词替换:   1.Improve 提高:   Promote: 促进AC之间的贸易 promote the trade between ...

  2. [JS] 限制上传文件的类型和大小

    <!DOCTYPE html> <!-- saved from url=(0035)http://localhost:9090/qraved/update --> <ht ...

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

    20145224陈颢文<Java程序设计>第二周学习总结 教材学习内容总结 一.类型.变量与运算符 1.类型 整数: 可细分为为short整数(占2字节),int整数(占4字节),long ...

  4. jquery和js使用技巧

    1. 如何得知图片已加载完毕 这也一个没有很好文档说明的问题(至少在我查找时没看到),但是在创建照片库.旋转灯笼效果等方面,它是相当常见的需求.而这在jQuery中很容易实现. 所有你要做的就是在IM ...

  5. java多线程的常用方法(以及注意事项)

    /* * 线程的常用方法 * 1.start(); * 2.run(); * 3.sleep(int millsecond); * 4.isAlive(); -->判断线程是否还在运行 * 5. ...

  6. 快速集成iOS基于RTMP的视频推流

    前言 这篇blog是iOS视频直播初窥:<喵播APP>的一个补充. 因为之前传到github上的项目中没有集成视频的推流.有很多朋友简信和微博上问我推流这部分怎么实现的. 所以, 我重新集 ...

  7. Java 集合系列 12 TreeMap

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  8. 对石家庄铁道大学网站的UI分析

    学校网站的首页面的色调用的比较好看,用深蓝色体现了严谨治学的风范.然后就是网站的首页有 1,学校概况:学校简介 现任领导 历任领导 校史沿革 2,组织机构: 机构设置 院系设置 管理机构 直属单位 其 ...

  9. PL/SQL设置编码方式

    (2012-10-30 21:38:33) 转载▼ 标签: 杂谈 分类: ORACLE 导出sql文件出现乱码问题,百度之后,发现问题是由于PL/SQL客户端和ORACLE的字符编码设置不一致引起的. ...

  10. node 事件循环

    什么是事件循环 Node只运行在一个单一线程上,至少从Node.js开发者的角度是这样的.在底层, Node是通过libuv来实现多线程的. Libuv库负责Node API的执行.它将不同的任务分配 ...