关于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命令,如打开一个记事本
- package com.iwtxokhtd.other;
- import java.io.IOException;
- public class ProcessTest {
- public static void main(String[] args) {
- try {
- Process proc=Runtime.getRuntime().exec("notepad");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
(2)使用它的其它构造方法执行相关的命令,如下例:

- package com.iwtxokhtd.other;
- import java.io.IOException;
- public class ProcessTest {
- public static void main(String[] args) {
- try {
- String exeFullPathName="C:/Program Files/Internet Explorer/IEXPLORE.EXE";
- String message="www.google.com";
- String []cmd={exeFullPathName,message};
- Process proc=Runtime.getRuntime().exec(cmd);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
执行上述命令可以打开Google网站
(3)列出系统正在运行的所有进程信息

- package com.iwtxokhtd.other;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ListAllProcessTest {
- //列出所有的进程信息
- public static void main(String[] args) {
- BufferedReader br=null;
- try {
- Process proc=Runtime.getRuntime().exec("tasklist");
- br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
- @SuppressWarnings("unused")
- String line=null;
- System.out.println("打印所有正在运行的进程信息");
- while((line=br.readLine())!=null){
- System.out.println(br.readLine());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(br!=null){
- try {
- br.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
(4)判断一个具体的进程是否正在运行,如下例:

- package com.iwtxokhtd.other;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class FindProcessExeTest
- {
- public static void main(String []args){
- if(findProcess("QQ.exe")){
- System.out.println("------判断指定的进程是否在运行------");
- System.out.println("QQ.exe该进程正在运行!");
- }else{
- System.out.println("------判断指定的进程是否在运行------");
- System.out.println("QQ.exe该进程没有在运行!");
- }
- }
- public static boolean findProcess(String processName){
- BufferedReader br=null;
- try{
- //下面这句是列出含有processName的进程图像名
- Process proc=Runtime.getRuntime().exec("tasklist /FI /"IMAGENAME eq "+processName+"/"");
- br=new BufferedReader(new InputStreamReader(proc.getInputStream()));
- String line=null;
- while((line=br.readLine())!=null){
- //判断指定的进程是否在运行
- if(line.contains(processName)){
- return true;
- }
- }
- return false;
- }catch(Exception e){
- e.printStackTrace();
- return false;
- }finally{
- if(br!=null){
- try{
- br.close();
- }catch(Exception ex){
- }
- }
- }
- }
- }
关于process的更多相关文章
- IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页
因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...
- C#的Process类调用第三方插件实现PDF文件转SWF文件
在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...
- C# ShellExcute与Process
C#运行外部程序的两种方法 ShellExecute using System.Runtime.InteropServices; public enum ShowWindowCommands : in ...
- 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方
刚上线一个新版本,其中有台电脑打开软件就报[xx的类型初始值设定项引发异常](还好不是一大波电脑,新东西上线就怕哀鸿遍野),如图: 显然是该类型的静态构造函数中抛异常了(红线处就是类名),遂打开该类, ...
- C# - 多线程 之 Process与Thread与ThreadPool
Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...
- Java Business Process Management(业务流程管理) 初识环境搭建
一.简介 (一)什么是jbpm JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易 ...
- 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 ...
- 更新过程 renewal process
一类随机过程.是描述元件或设备更新现象的一类随机过程.设对某元件的工作进行观测.假定元件的使用寿命是一随机变量,当元件发生故障时就进行修理或换上新的同类元件,而且元件的更新是即时的(修理或更换元件所需 ...
- Transaction (Process ID xxx) was deadlocked on lock
Transaction (Process ID 161) was deadlocked on lock | communication buffer resources with another pr ...
- NodeJS入门(五)—— process对象
process对象用于处理与当前进程相关的事情,它是一个全局对象,可以在任何地方直接访问到它而无需引入额外模块. 它是 EventEmitter 的一个实例. 本章的示例可以从我的Github上下载到 ...
随机推荐
- 在Ubuntu 14 上安装 Nginx-RTMP 流媒体服务器
一:RTMP RTMP流媒体协议是 一套 Adobe 开发的音频视频实时传输协议: 二:Nginx-rtmp nginx-rtmp 是一个基于nginx的 RTMP服务模块,开源,免费 https:/ ...
- 客户端ARPG角色行为模型
[概述] 对于玩家自身而言, 场景中的角色分两种:自己,别的生物(包括别的玩家,monster, npc等).而生物本身是一个集合{属性数据(状态), 行为(动作表现)}. 站在玩家自身的角度来看, ...
- noip2016赛后总结
面前并不是一颗变质的心. 只是一种综合并适应一切的情怀. 这或许是最好的心态... 今年的noip貌似考得好不理想呢...彻底挂了... gjs580,hgr555. 一个初三学弟570,一个400+ ...
- 第四周 更新Scrum站立会议
项目名称:连连看游戏(C#) 小组名称:4Boys 小组成员:武志远.李权.张金生.张政 站立会议内容 昨天完成的: 1.张金生主要介绍了自己完成了游戏界面 2.武志远主要负责查阅关于技术方面的资料, ...
- 【转载】FLUNT温度场模拟
1. Gambit 绘制几何计算域,划分网格,定义边界类型 2. fluent设置以及计算 注意: define->models->energy 打开能量方程 de ...
- struts2视频学习笔记 15-17 (访问或添加request属性,文件上传)
课时15 访问或添加request/session/application属性 1.简单说 page指当前页面.在一个jsp页面里有效 2.request 指从http请求到服务器处理结束,返回响应的 ...
- css+div如何解决文字溢出
看到标题你一定很轻易就会想到截断文字加“...”的做法.哈哈,就是这样.其实写这篇日志也只是把这样方法做个记录,因为似乎还有很多人不记得碰到这样的情况该如何处理. 首先,先解释一下,一般用div+cs ...
- oracle数据泵备份(Expdp命令)[转]
Oracle备份方式主要分为数据泵导出备份.热备份与冷备份三种,今天首先来实践一下数据泵备份与还原.数据泵导出/导入属于逻辑备份,热备份与冷备份都属于物理备份.oracle10g开始推出了数据泵( ...
- 六种流行的语言---C、C++、python、Java、php、C#比较[转]
语言大餐 回归正题,本文是六种语言连接mysql数据库的代码展示,在LZ尝试的过程中,无论是语言环境搭建.mysql依赖库的导入还是代码的风格,各种语言都各有千秋.接下来,我们就让这些语言一一登场吧. ...
- apache 访问权限基本设置
1 .禁止访问某个或多个文件夹 在.htaccess文件里面写入 RewriteRule ^foldername - [F,L] #禁止访问某个文件夹 RewriteRule ^ ...