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. SAM格式 及 比对工具之 samtools 使用方法

    参考资料: SAMtools(官网) SAM Spec v1.4 (SAM格式 说明书) (重要) samtools-1.3.1 使用手册 (SAMtools软件说明书) samtools常用命令详解 ...

  2. 串行通讯之.NET SerialPort异步写数据

    目录 第1章说明    2 1 为什么需要异步写数据?    2 2 异步写数据的代码    2 3 源代码    4 第1章说明 1 为什么需要异步写数据? 如下图所示,以波特率300打开一个串口. ...

  3. 集成代码编辑器ACE的经验

    ACE是最流行的在线代码编辑器之一,在CanTK的集成开发环境GameBuilder里也使用了ACE.ACE的功能非常强大,但是由于使用方法不当,大家反映GameBuilder的代码编辑器不好用.最近 ...

  4. 纯css3代码写下拉菜单效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. JS实现会动的小车

    2015-06-05怎么说呢,我想要实现的功能是很简单的,但是过程中,遇到不少问题. 我要实现的功能是页面右侧有辆小车,鼠标滚动或者拉动滚动条,小车消失,在底部点击“返还顶部”按钮后,页面缓慢向上滚动 ...

  6. [Hadoop 周边] 浅谈大数据(hadoop)和移动开发(Android、IOS)开发前景【转】

    原文链接:http://www.d1net.com/bigdata/news/345893.html 先简单的做个自我介绍,我是云6期的,黑马相比其它培训机构的好偶就不在这里说,想比大家都比我清楚: ...

  7. linux开关端口问题

    linux开关端口问题: 我们知道一些常用的端口,比如mysql的端口为3306,sql的端口为:1433,以及tomcat的端口为 8008等等一样! 当这些端口在linux下是没有开启时,我们是无 ...

  8. Unique Binary Search Trees [LeetCode]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. C++格式化输入输出

    要实现格式化输入输出,程序需要包含 iostreams 标准标头 <iomanip> 以定义几个各自采用单个参数的操控器. 备注: 其中每个操控器都返回重载 basic_istream&l ...

  10. vs版本与.net framework 版本对应

    vs2002  .net framework 1.0 vs2003 版本号:7.x  .net framework 1.1   window server 2003 vs2005 版本号:8.x  . ...